Thibaut D.
Thibaut D.

Reputation: 2663

What happens to running threads after forking?

I'm using OpenERP, a Python based ERP, which uses different threads (one-thread per client, etc). I would like to use multiprocessing.Process() to fork() and call a long-running method.

My question is: what will happen to the parent's threads? Will they be copied and continue to run? Will the child process call accept() on the server socket?

Thanks for your answers,

Upvotes: 2

Views: 772

Answers (1)

Rostyslav Dzinko
Rostyslav Dzinko

Reputation: 40825

Forking does not copy threads, only the main one. So be very careful with forking multithreaded application as it can cause unpredictable side-effects (e.g when forking happened while some thread was executing in a mutexed critical section), something really can be broken in your forked process unless you know the code you're forking ideally.

Though everything that I said above is true, there's a workaround (at least on Linux) called pthread_atfork() which acts as a callback when a process was forked (you can recreate all needed threads). Though it applies to C applications, it's not applied to Python ones.

For further information you can refer to:

Upvotes: 4

Related Questions