[英]Python Subprocess not starting but no errors either
I'd like to have a script that creates 2 simultaniuosly running while loops.我想要一个脚本来创建 2 个同时运行的 while 循环。 To achieve that I am trying to open another script (to have multiple scripts running at the same time instead of having all the code in one).
为了实现这一点,我正在尝试打开另一个脚本(同时运行多个脚本,而不是将所有代码都放在一个脚本中)。 Main script:
主要脚本:
#!/bin/env python
import sys
import subprocess
print sys.path
process = subprocess.Popen('/home/pi/test2.py', shell=True, stdout=subprocess.PIPE)
Second script:第二个脚本:
#!/bin/env python
import sys
i=1
print sys.path
while i<50:
print i
i=i+1
The main script terminates without errors but it looks like the second doesn't even start.主脚本没有错误地终止,但看起来第二个脚本甚至没有启动。 Why?
为什么?
If you want to see the output of test2.py
then you shouldn't specify stdout=subprocess.PIPE
.如果您想查看
test2.py
的输出,则不应指定stdout=subprocess.PIPE
test2.py
。 That collects the standard out and allows you to access it via process
object.收集标准输出并允许您通过
process
对象访问它。
Also, for what it's worth, shell=True
isn't a good idea unless you're certain you need it.此外,就其价值而言,
shell=True
不是一个好主意,除非您确定需要它。
It will then look like this然后它看起来像这样
subprocess.Popen(['/home/pi/test2.py'])
Adding to sigmavirus24s answer.添加到 sigmavirus24s 答案。 When you don't set
shell=True
you have to pass the arguments as a list like this ['/home/pi/test2.py']
and pass that as the first argument.当您不设置
shell=True
您必须将参数作为这样的列表传递['/home/pi/test2.py']
并将其作为第一个参数传递。 For many arguments use the shlex
modules split
function that splits a string
to a list of arguments for you.对于许多参数,请使用
shlex
modules split
函数将string
拆分为参数列表。
Edit: For the script to work you need to pass ['python', '/home/pi/test2.py']
as the argument.编辑:要使脚本正常工作,您需要将
['python', '/home/pi/test2.py']
作为参数传递。 This tells python
to run the script.这告诉
python
运行脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.