Python child process in while True loop blocks parent

Jen Kris jenkris at tutanota.com
Mon Dec 6 16:05:48 EST 2021


Here is what I don't understand from what you said.  "The child process is created with a single thread—the one that called fork()."  To me that implies that the thread that called fork() is the same thread as the child process.  I guess you're talking about the distinction between logical threads and physical threads.  

But the main issue is your suggestion that I should call fork-execv from the thread that runs the main C program, not from a separate physical pthread.  That would certainly eliminate the overhead of creating a new pthread. 

I am working now to finish this, and I will try your suggestion of calling fork-execv from the "main" thread.  When I reply back next I can give you a complete picture of what I'm doing. 

Your comments, and those of Peter Holzer and Chris Angelico, are most appreciated. 




Dec 6, 2021, 10:37 by barry at barrys-emacs.org:

>
>
>> On 6 Dec 2021, at 17:09, 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()."
>>
>
> You just quoted the evidence!
>
> All new processes on unix (may all OS) only ever have one thread when they start.
> The thread-id of the first thread is the same as the process-id and referred to as the main thread.
>
>>
>> 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.
>>
>
>
> By that logic it can only run one process...
>
> It has one hardware core that support one hardware thread.
> Linux can create as many software threads as it likes.
>
>> You may also want to see "Forking vs Threading" (https://www.geekride.com/fork-forking-vs-threading-thread-linux-kernel), "Fork vs Thread" (https://medium.com/obscure-system/fork-vs-thread-38e09ec099e2), and "Linux process and thread" (https://zliu.org/post/linux-process-and-thread) ("This means that to create a normal process fork() is used that further calls clone() with appropriate arguments while to create a thread or LWP, a function from pthread library calls clone() with relvant flags. So, the main difference is generated by using different flags that can be passed to clone() funciton(to be exact, it is a system call"). 
>>
>> You may be confused by the fact that threads are called light-weight processes.
>>
>
> No Peter and I are not confused.
>
>>
>> Or maybe I'm confused :)
>>
>
> Yes you are confused.
>
>>
>> If you have other information, please let me know.  Thanks.
>>
>
> Please get the book I recommended, or another that covers systems programming on unix, and have a read.
>
> Barry
>
>>
>> Jen
>>
>>
>> Dec 5, 2021, 18:08 by hjp-python at hjp.at:
>>
>>> On 2021-12-06 00:51:13 +0100, Jen Kris via Python-list wrote:
>>>
>>>> The C program creates two threads (using pthreads), one for itself and
>>>> one for the child process.  On creation, the second pthread is pointed
>>>> to a C program that calls fork-execv to run the Python program.  That
>>>> way Python runs on a separate thread. 
>>>>
>>>
>>> I think you have the relationship between processes and threads
>>> backwards. A process consists of one or more threads. Fork creates a new
>>> process and therefore also a new thread.
>>>
>>> hp
>>>
>>> -- 
>>> _  | Peter J. Holzer    | Story must make more sense than reality.
>>> |_|_) |                    |
>>> | |   | hjp at hjp.at         |    -- Charles Stross, "Creative writing
>>> __/   | http://www.hjp.at/ |       challenge!"
>>>
>>
>> -- 
>> https://mail.python.org/mailman/listinfo/python-list
>>



More information about the Python-list mailing list