Threading plus multiprocessing plus cv2 error

Karen Shaeffer klsshaeffer at icloud.com
Sun Aug 30 03:54:19 EDT 2020



> On Aug 29, 2020, at 10:12 PM, Stephane Tougard via Python-list <python-list at python.org> wrote:
> 
> On 2020-08-29, Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote:
>> 	Under Linux, multiprocessing creates processes using fork(). That means
>> that, for some fraction of time, you have TWO processes sharing the same
>> thread and all that entails (if it doesn't overlay the forked process with
>> a new executable, they are sharing the thread until the thread exits).
>> same error condition even with the sleep(1) in place.
> 
> I'm not even that makes sense, how 2 processes can share a thread ?
> 

Hello,
On linux, fork is a kernel system call. The linux kernel creates two identical processes running in separate memory spaces. At the time of creation, these memory spaces have the same content. There are some issues to be aware of. Just type ‘man fork’ on the command line of a linux system, and you can read about the issues of concern, presuming you have installed the manual pages for the linux kernel system calls.

If the forked process doesn’t overlay onto a separate memory space, then the fork system call fails, returning a failure code to the parent process. When the linux kernel is executing the fork system call, the parent (forking process) is blocked on the system call. The linux kernel actually takes over the parent process during execution of the system call, running that process in kernel mode during the execution of the fork process. The parent (forking) process only restarts, after the kernel returns.

On linux, within a given process, threads share the same memory space. If that process is the python interpreter, then the Global lock ensures only one thread is running when the fork happens. After the fork, then you have two distinct processes running in two separate memory spaces. And the fork man page discusses the details of concern with regards to specific kernel resources that could be referenced by those two distinct processes. The thread context is just a detail in that respect. All the threads of the parent process that forked the new process all share the same parent memory space.

humbly,
kls



More information about the Python-list mailing list