Is the subprocess module thread-safe?

J

John Nagle

Can the subprocess module be used to launch subprocesses from threads?
Or does the subprocess module affect the entire process context, like "fork"?
Or is this OS dependent?

Also, if you launch processes from the subprocess module module and
don't wait them to complete, do zombies accumulate under UNIX/Linux?

John Nagle
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Can the subprocess module be used to launch subprocesses from threads?

The answer to this question ("can") is a clear yes. Notice that this
question is different from the question in the subject.
Or does the subprocess module affect the entire process context, like
"fork"?

On POSIX systems, it uses fork(2) - what else could it do? I'm not sure
what effects to the entire process context you are referring to. In
a POSIX system, you can also fork from a thread just fine.
Or is this OS dependent?

The precise semantics are OS dependent, yes. However, you can use it
in the presence of threads on most systems that support threads (for
a reasonable definition of "supports threads", which may exclude
HP-UX and FreeBSD :)

It's technically not thread-safe if other threads manipulate file
descriptors; there is always a chance of race conditions when some
thread opens a file handle and then tries to set the CLOEXEC flag
on it - if some other thread creates a subprocess in-between, there
is a chance that the child process "sees" the file handle even though
it "should" have the CLOEXEC flag set (but didn't at the point when
the process forked).
Also, if you launch processes from the subprocess module module and
don't wait them to complete, do zombies accumulate under UNIX/Linux?

No. It depends on the precise code, but if you drop the reference to the
Popen object, or call one of the functions that don't return one in the
first place, the module itself will wait for the child processes
"regularly".

Regards,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top