Weird behaviour with threads and processes mixing
<p>I'm running the following python code:</p>

<pre><code>import threading
import multiprocessing

def forever_print():
    while True:
        print("")

def main():
    t = threading.Thread(target=forever_print)
    t.start()
    return


if __name__=='__main__':
    p = multiprocessing.Process(target=main)
    p.start()
    p.join()
    print("main process on control")
</code></pre>

<p>It terminates. </p>

<p>When I unwrapped <code>main</code> from the new process, and just ran it directly, like this:</p>

<pre><code>if name == '__main__':
    main()
</code></pre>

<p>The script went on forever, as I thought it should. Am I wrong to assume that, given that <code>t</code> is a non-daemon process, <code>p</code> shouldn't halt in the first case?</p>

<p>I basically set up this little test because i've been developing an app in which threads are spawned inside subprocesses, and it's been showing some weird behaviour (sometimes it terminates properly, sometimes it doesn't). I guess what I wanted to know, in a broader sense, is if there is some sort of "gotcha" when mixing these two python libs.</p>

<p>My running environment: python 2.7 @ Ubuntu 14.04 LTS</p>

<p>For now, threads created <em>by</em> <code>multiprocessing</code> worker processes act like daemon threads with respect to process termination:  the worker process exits without waiting for the threads it created to terminate.  This is due to worker processes using <code>os._exit()</code> to shut down, which skips most normal shutdown processing (and in particular skips the normal exit processing code (<code>sys.exit()</code>) that <code>.join()</code>'s non-daemon <code>threading.Thread</code>s).</p>

<p>The easiest workaround is for worker processes to explicitly <code>.join()</code> the non-daemon threads they create.</p>

<p>There's an open bug report about this behavior, but it hasn't made much progress:  <a href="http://bugs.python.org/issue18966" rel="nofollow">http://bugs.python.org/issue18966</a></p>

python multithreading multiprocessing