简体   繁体   中英

Python Subprocess not starting but no errors either

I'd like to have a script that creates 2 simultaniuosly running while loops. 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 . That collects the standard out and allows you to access it via process object.

Also, for what it's worth, shell=True isn't a good idea unless you're certain you need it.

It will then look like this

subprocess.Popen(['/home/pi/test2.py'])

Adding to sigmavirus24s answer. 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. For many arguments use the shlex modules split function that splits a string to a list of arguments for you.

Edit: For the script to work you need to pass ['python', '/home/pi/test2.py'] as the argument. This tells python to run the script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM