How to torknutsya in the background at the same time conveying pipe in python34?

Now come to this option:
import subprocess

p = subprocess.Popen([second.py], stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, universal_newlines=True)

try:
 p.communicate(input='one\ntwo\nthree', timeout=1)
except:
 pass

With him everything works, but if you set a very small timeout (0 or 0.00000001) then second.py will not start.

Can there be any problems with this approach (such as greatly hindering the computer, does not have time to start a second script)? Is there a more proper way is to fork (using Popen)?
August 23rd 19 at 10:46
1 answer
August 23rd 19 at 10:48
It is not clear what is required to solve it. Method communicate - blocking, it will wait for the child process to exit. If this is not desired, then communicate is not necessary to call: the main script will finish its work, and the child (if it is a long operation) will be replaced by pid the pid of the init process (launchd in Mac, Ubuntu and systemd, etc.). As this is purely and rightly so - depends on the child script: he will be responsible for their own clean shutdown, logs, etc.

If, however, you has to wait for the child process to exit, you must communicate with the call timeout is sufficient for testing the script a child process, i.e. not to reduce it to obviously insufficient to meet the child process variables, because when reaching the timeout the child process will receive a forced terminate.

Tip: experiment! Put the tool htop and monitor the behavior of the parent and child processes. Make them indefinitely using the while True and sleep. Try to see in htop when the situation is replaced by the pid on the pid of init (as described above), try to create a zombie process. Good luck.

Find more questions by tags Python