我正在为用户创建一个小仪表板,以便他能够运行特定的工作.我正在使用Django所以我希望他能够单击一个链接来启动作业,然后将页面返回给他,并显示作业正在运行的消息.该工作的结果将在稍后通过电子邮件发送给他.
我相信我应该使用,subprocess.Popen但我不确定.所以在伪代码中,这就是我想要做的:
if job == 1:
run script in background: /path/to/script.py
return 'Job is running'
Run Code Online (Sandbox Code Playgroud) 在Windows上,subprocess.Popen.terminate调用win32 TerminalProcess.但是,我看到的行为是我尝试终止的进程的子进程仍在运行.这是为什么?如何确保进程启动的所有子进程都被终止?
我想从执行Test_Pipe.py输出,我尝试在Linux上使用代码,但它没有用.
Test_Pipe.py
import time
while True :
print "Someting ..."
time.sleep(.1)
Run Code Online (Sandbox Code Playgroud)
Caller.py
import subprocess as subp
import time
proc = subp.Popen(["python", "Test_Pipe.py"], stdout=subp.PIPE, stdin=subp.PIPE)
while True :
data = proc.stdout.readline() #block / wait
print data
time.sleep(.1)
Run Code Online (Sandbox Code Playgroud)
该行proc.stdout.readline()被阻止,因此没有数据打印出来.
我正在使用Popen调用一个shell脚本,该脚本不断将其stdout和stderr写入日志文件.有没有办法连续同时输出日志文件(到屏幕),或者让shell脚本同时写入日志文件和标准输出?
我基本上想在Python中做这样的事情:
cat file 2>&1 | tee -a logfile #"cat file" will be replaced with some script
Run Code Online (Sandbox Code Playgroud)
再次,这将stderr/stdout连接到tee,将它写入stdout和我的日志文件.
我知道如何在Python中将stdout和stderr写入日志文件.我被困在哪里是如何将这些复制回屏幕:
subprocess.Popen("cat file", shell=True, stdout=logfile, stderr=logfile)
Run Code Online (Sandbox Code Playgroud)
当然我可以做这样的事情,但有没有办法在没有tee和shell文件描述符重定向的情况下做到这一点?:
subprocess.Popen("cat file 2>&1 | tee -a logfile", shell=True)
Run Code Online (Sandbox Code Playgroud) 我想在后台打开一个进程并与之交互,但这个进程在Linux和Windows中都应该是不可见的.在Windows中,您必须使用STARTUPINFO执行某些操作,而这在Linux中无效:
ValueError:仅在Windows平台上支持startupinfo
有没有比为每个操作系统创建单独的Popen命令更简单的方法?
if os.name == 'nt':
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
proc = subprocess.Popen(command, startupinfo=startupinfo)
if os.name == 'posix':
proc = subprocess.Popen(command)
Run Code Online (Sandbox Code Playgroud) PHP有两个密切相关的函数,escapeshellarg()和escapeshellcmd().他们似乎都做类似的事情,就是帮助一个字符串更安全的使用system()/ exec()的/ etc.
我应该使用哪一个?我只是希望能够接受一些用户输入并在其上运行命令,而不是让一切都爆炸.如果PHP有一个exec-type-function,它接受了一个绕过shell的字符串数组(比如argv),我会使用它.类似于Python的subprocess.call()功能.
我试过这样的事情:
subprocess.Popen(['nohup', 'my_command'],
stdout=open('/dev/null', 'w'),
stderr=open('logfile.log', 'a'))
Run Code Online (Sandbox Code Playgroud)
如果父脚本正常退出,则此方法有效,但如果我终止脚本(Ctrl-C),则所有子进程也将被终止.有办法避免这种情况吗?
我关心的平台是OS X和Linux,使用的是Python 2.6 和 Python 2.7.
我想将子进程的stderr输出重定向到stdout.常数STDOUT应该这样做,不应该吗?
然而,
$ python >/dev/null -c 'import subprocess;\
subprocess.call(["ls", "/404"],stderr=subprocess.STDOUT)'
Run Code Online (Sandbox Code Playgroud)
做输出的东西.为什么会这样,我如何在stdout上收到错误消息?
完整的工作测试案例
当然,根据您在本地和远程计算机上的内存,您的阵列大小会有所不同.
z1 = numpy.random.rand(300000000,2);
for i in range(1000):
print('*******************************************\n');
direct_output = subprocess.check_output('ssh blah@blah "ls /"', shell=True);
direct_output = 'a'*1200000;
a2 = direct_output*10;
print(len(direct_output));
Run Code Online (Sandbox Code Playgroud)
当前用例
如果它有助于我的用例如下:
我发出数据库查询,然后将结果表存储在远程计算机上.然后我想通过网络传输它们并进行分析.到目前为止,我在python中做了类似下面的事情:
#run a bunch of queries before hand with the results in remote files
....
counter = 0
mergedDataFrame = None
while NotDone:
output = subprocess.check_output('ssh blah@blah cat /data/file%08d'%(counter))
data = pandas.read_csv(...)
#do lots of analysis, append, merge, numpy stuff etc...
mergedDataFrame = pandas.merge(...)
counter += 1
Run Code Online (Sandbox Code Playgroud)
在某些时候,我在check_output命令中收到以下错误:[Errno 12]无法分配内存
背景
感谢以下问题,我想我已经知道出了什么问题.发布了许多解决方案,我试图确定哪些解决方案将避免[Errno 12]无法使用fork/clone分配与子进程实现相关的内存错误.
当执行Python文档中subprocess.run()给出的时,我得到一个TypeError:
>>> import subprocess
>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.6/subprocess.py", line 403, in run
with Popen(*popenargs, **kwargs) as process:
TypeError: __init__() got an unexpected keyword argument 'capture_output'
Run Code Online (Sandbox Code Playgroud)
我正在运行Python 3.6.6:
$ python3 --version
Python 3.6.6
Run Code Online (Sandbox Code Playgroud) subprocess ×10
python ×9
process ×3
linux ×2
popen ×2
windows ×2
background ×1
django ×1
exec ×1
kill-process ×1
memory ×1
networking ×1
nohup ×1
paramiko ×1
php ×1
shell ×1
stderr ×1