Note this question is not the same as Python Subprocess.Popen from a thread, because that question didn't seek an explanation on why it is ok.
If I understand correctly, subprocess.Popen()
creates a new process by forking the current process and execv new program.
However, if the current process is multithreaded, and we call subprocess.Popen()
in one of the thread, won't it be duplicating all the threads in the current process (because it calls syscall fork()
)? If it's the case, though these duplicated threads will be wiped out after syscall execv
, there's a time gap in which the duplicated threads can do a bunch of nasty stuff.
A case in point is gtest_parallel.py, where the program creates a bunch of threads in execute_tasks()
, and in each thread task_manager.run_task(task)
will call task.run()
, which calls subprocess.Popen()
to run a task. Is it ok?
The question applies to other fork-in-thread programs, not just Python.