简体   繁体   中英

how to run a different process on a different thread and get the output to the first thread? python

I want from this script to run another python script (scriptA.py) I tried process = subprocess.popen(cmd, stdout=subprocess.PIPE) and then iter on the output lines..

and also tried this solution https://gist.github.com/kirpit/1306188

but not really sure how to implement it..

I want to open scriptA.py using code from the main file (subprocess.popen etc) but to open it on another thread from the main file.. because they both need to do things in parallel..

scriptA.py will pull information from an android device.. and SM will issue commands to the same device according to the output from scriptA.py..

Any ideas?

Thanks

By default subprocess.Popen runs the child program in a new process. So this won't block your main process at all.

childProc = subprocess.Popen(cmd ..)

while True:
    childProc.poll() # to see if it has finished 
    if childProc.returncode:
        break # Finished
    # communicate somedata and read stdout and stderrs
    outs, errs = childProc.communicate(input="somedata")

PS : The infinite loop is just for presenting the concept

First I'd read that: What is the difference between a process and a thread?

You need to figre if it's a thread or a process you need. If you really want a process you need to spawn a child process then wait for it to end and read the output. This might help: wait process until all subprocess finish?

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