我如何实现一些逻辑,允许我fork()使用Python 在Windows上使用系统调用在Linux上重现我的功能?
我特意尝试在SAPI Com组件上执行一个方法,同时继续主线程中的其他逻辑而不阻塞或等待.
我使用以下命令运行子进程:
p = subprocess.Popen("subprocess",
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE)
Run Code Online (Sandbox Code Playgroud)
此子进程可以立即退出stderr上的错误,也可以继续运行.我想检测这些条件中的任何一个 - 后者等待几秒钟.
我试过这个:
SECONDS_TO_WAIT = 10
select.select([],
[p.stdout, p.stderr],
[p.stdout, p.stderr],
SECONDS_TO_WAIT)
Run Code Online (Sandbox Code Playgroud)
但它只是返回:
([],[],[])
Run Code Online (Sandbox Code Playgroud)
无论哪种条件.我能做什么?
我正在使用python来管理一些模拟.我构建参数并使用以下命令运行程序:
pipe = open('/dev/null', 'w')
pid = subprocess.Popen(shlex.split(command), stdout=pipe, stderr=pipe)
Run Code Online (Sandbox Code Playgroud)
我的代码处理不同的信号.Ctrl + C将停止模拟,询问我是否要保存,然后正常退出.我有其他信号处理程序(例如强制数据输出).
我想要的是向我的python脚本发送一个信号(SIGINT,Ctrl + C),它将询问用户他想要发送给程序的信号.
阻止代码工作的唯一因素是,无论我做什么,Ctrl + C都将"转发"到子进程:代码将捕获并退出:
try:
<wait for available slots>
except KeyboardInterrupt:
print "KeyboardInterrupt catched! All simulations are paused. Please choose the signal to send:"
print " 0: SIGCONT (Continue simulation)"
print " 1: SIGINT (Exit and save)"
[...]
answer = raw_input()
pid.send_signal(signal.SIGCONT)
if (answer == "0"):
print " --> Continuing simulation..."
elif (answer == "1"):
print " --> Exit and save."
pid.send_signal(signal.SIGINT)
[...]
Run Code Online (Sandbox Code Playgroud)
所以无论我做什么,程序都会收到我只希望我的python脚本看到的SIGINT.我怎样才能做到这一点???
我也尝试过: …
我需要执行一个产生大量输出的命令,并且需要花费大量时间来执行(> 30分钟).我正在考虑使用subprocess.Popen来做到这一点.我需要捕获命令的输出,所以我将PIPE传递给stdout和stderr.
使用Popen.wait()时的死锁问题已在很多论坛上得到充分记录,因此Popen.communicate()是避免死锁的建议方法.该解决方案的问题是communication()阻塞直到命令完成.我需要在执行命令时打印到达stdout的所有内容.如果20分钟后没有输出,脚本执行将被终止.
以下是我需要遵守的一些约束:
有办法吗?
我看了很多问题,但仍然无法弄清楚这一点.我正在使用PyQt,我希望运行ffmpeg -i file.mp4 file.avi并获取输出,因为我可以创建一个进度条.
我看过这些问题: ffmpeg可以显示进度条吗? 从子进程实时捕获stdout
我能够使用以下代码查看rsync命令的输出:
import subprocess, time, os, sys
cmd = "rsync -vaz -P source/ dest/"
p, line = True, 'start'
p = subprocess.Popen(cmd,
shell=True,
bufsize=64,
stdin=subprocess.PIPE,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE)
for line in p.stdout:
print("OUTPUT>>> " + str(line.rstrip()))
p.stdout.flush()
Run Code Online (Sandbox Code Playgroud)
但是当我更改命令时,ffmpeg -i file.mp4 file.avi我没有收到任何输出.我猜这与stdout/output缓冲有关,但是我不知道如何读取看起来像的行
frame= 51 fps= 27 q=31.0 Lsize= 769kB time=2.04 bitrate=3092.8kbits/s
Run Code Online (Sandbox Code Playgroud)
我可以用来计算进度.
有人可以告诉我一个如何从ffmpeg获取此信息到python的示例,无论是否使用PyQt(如果可能)
编辑: 我最终使用jlp的解决方案,我的代码看起来像这样:
#!/usr/bin/python
import pexpect
cmd = 'ffmpeg -i file.MTS file.avi'
thread = pexpect.spawn(cmd)
print "started %s" % cmd …Run Code Online (Sandbox Code Playgroud) 我希望这不是重复.
我正在尝试使用subprocess.Popen()在单独的控制台中打开脚本.我已经尝试过设置shell=True参数,但这并不能解决问题.
我在64位Windows 7上使用32位Python 2.7.
是否可以修改以下代码以从'stdout'和'stderr'打印输出:
代码:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import subprocess
def run_cmd(command, cwd=None):
p = subprocess.Popen(command, cwd=cwd, shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
outs, errs = p.communicate()
rc = p.returncode
outs = outs.decode('utf-8')
errs = errs.decode('utf-8')
return (rc, (outs, errs))
Run Code Online (Sandbox Code Playgroud)
感谢@unutbu,特别感谢@ jf-sebastian,最终功能:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from queue import Queue
from subprocess import PIPE, Popen
from threading import Thread
def read_output(pipe, funcs):
for line in iter(pipe.readline, b''):
for func in funcs: …Run Code Online (Sandbox Code Playgroud) 我正在阅读Python内存管理,并希望减少我的应用程序的内存占用.有人建议,子程序在减轻问题方面会有很长的路要走; 但我无法概念化需要做什么.有人可以提供一个简单的例子来说明如何...
def my_function():
x = range(1000000)
y = copy.deepcopy(x)
del x
return y
@subprocess_witchcraft
def my_function_dispatcher(*args):
return my_function()
Run Code Online (Sandbox Code Playgroud)
...进入一个真正的子处理函数,不存储额外的"自由列表"?
这个"自由列表"概念是否也适用于python c-extensions?
我在AIX 6.1上运行并使用Python 2.7.想要执行以下行但收到错误.
subprocess.run(["ls", "-l"])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'run'
Run Code Online (Sandbox Code Playgroud) 从文档中的示例subprocess.run()来看,似乎不应该有任何输出
subprocess.run(["ls", "-l"]) # doesn't capture output
Run Code Online (Sandbox Code Playgroud)
但是,当我在python shell中尝试它时,列表被打印出来.我想知道这是否是默认行为以及如何抑制输出run().