Python child process in while True loop blocks parent

Chris Angelico rosuav at gmail.com
Mon Dec 6 13:27:43 EST 2021


On Tue, Dec 7, 2021 at 4:10 AM Jen Kris via Python-list
<python-list at python.org> wrote:
>
> I can't find any support for your comment that "Fork creates a new
> process and therefore also a new thread."  From the Linux man pages https://www.man7.org/linux/man-pages/man2/fork.2.html, "The child process is created with a single thread—the one that called fork()."
>
> I have a one-core one-thread instance at Digital Ocean available running Ubuntu 18.04.  I can fork and create a new process on it, but it doesn't create a new thread because it doesn't have one available.
>

A CPU core is capable of running one or more threads *concurrently*,
and having multiple cores obviously multiplies that out. But for what
you're doing here, there's no material difference between threads
running concurrently and threads switching out between themselves
(other than performance, of course). From the operating system and
application perspectives, a "thread" is one runnable thread.

Every Unix process must contain a minimum of one thread. As a rough
rule of thumb, a process owns resources, a thread runs code. Without a
thread, you can't run any code.

Forking a process creates a child process and leaves the parent
running. The child process must by definition have a minimum of one
thread, and the man page is stating that one thread is all it gets.

Hope that helps explain things a bit. I don't know exactly what's
going on in your code, but maybe that'll clarify the word "thread".

ChrisA


More information about the Python-list mailing list