0

I found this thread which was over 2 years old. I wanted to ask a new question so I could get more attention

Non blocking subprocess.call

My question:

I have the same situation as @DavidJB as he describes in one of his comments in the earlier post

Thanks, this appears to work, however when I include a While loop in slave.py it seems get stuck and not perform anything in the loop (even with a timer.sleep() function..? – DavidJB Apr 17 '13 at 23:59

Anyone has any updates on this?

I want the child(slave.py), which has the while loop, to keep running and the parent(main.py) to not wait on the child to end. The parent should continue it's working.

This is how my main and child would look like.

main.py

...
proc = Popen('child.py', stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
out, err = proc.communicate()
...

In main, I want to spwan off the child.py and go do other stuff after spawning it off

child.py

while True:
    file = open( '/mnt/flash/child.txt', 'aw' )
    file.write( datetime.now().strftime('%Y-%m-%d %H:%M:%S') + "\t\topened the file now!!\thi\n"  )
    time.sleep(5)
    ...
    <break>
file.close()

The child, once started, writes into a file, the current time every 5 seconds unless some event breaks it out of the infinite loop

Community
  • 1
  • 1
  • Calling subprocess.Popen creates a new process just as if you run `python slave.py` from a shell. So there won't be a direct connection between the child and the parent unless you use eg. stdin=PIPE. AFAIK there is nothing else that can block the child. – cg909 Oct 07 '15 at 00:05
  • the comment is bogus. A while loop by itself won't hang the child process (it is a separate process). Could you [create a minimal code example that would demonstrate the issue](http://stackoverflow.com/help/mcve)? Describe what do you expect to happen and what happens instead (step by step). – jfs Oct 07 '15 at 15:37
  • I have edited the question, as per your request. @J.F. Sebastian – Sugetha Chandhrasekar Oct 07 '15 at 18:16
  • What is the issue? What did happen and what do you want to happen instead? (be exact) – jfs Oct 07 '15 at 18:30
  • I want the main to spawn off the child and continue it's execution. From than point on, I want the child to continue executing independently. Currently, the main waits until the child some how responds, (via the break statement). I do not want the main to wait on the child to return anything at all. All it has to do is spawn it off and continue. It works as expected if the child has no "while True". – Sugetha Chandhrasekar Oct 07 '15 at 18:41
  • I solved the issue. I removed the call to Popen.communicate() and it worked like a charm. – Sugetha Chandhrasekar Oct 07 '15 at 22:32
  • If you have removed `communicate()` then remove all PIPE too. Otherwise your child process may hang as soon as it fills the corresponding OS pipe buffers. – jfs Oct 08 '15 at 07:26

1 Answers1

-2

if you went to execute other python script so try this

main.py

 import subprocess

for insert, (list) in enumerate(list, start=1):

 l = [list]
  subprocess.call(["python", "slave.py", '%s' % l])
Najeeb Choudhary
  • 396
  • 1
  • 3
  • 21