Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <code>multiprocessing.pool.Pool</code> class creates the worker processes in its <code>__init__</code> method, makes them daemonic and starts them, and it is not possible to re-set their <code>daemon</code> attribute to <code>False</code> before they are started (and afterwards it's not allowed anymore). But you can create your own sub-class of <code>multiprocesing.pool.Pool</code> (<code>multiprocessing.Pool</code> is just a wrapper function) and substitute your own <code>multiprocessing.Process</code> sub-class, which is always non-daemonic, to be used for the worker processes.</p> <p>Here's a full example of how to do this. The important parts are the two classes <code>NoDaemonProcess</code> and <code>MyPool</code> at the top and to call <code>pool.close()</code> and <code>pool.join()</code> on your <code>MyPool</code> instance at the end.</p> <pre><code>#!/usr/bin/env python # -*- coding: UTF-8 -*- import multiprocessing # We must import this explicitly, it is not imported by the top-level # multiprocessing module. import multiprocessing.pool import time from random import randint class NoDaemonProcess(multiprocessing.Process): # make 'daemon' attribute always return False def _get_daemon(self): return False def _set_daemon(self, value): pass daemon = property(_get_daemon, _set_daemon) # We sub-class multiprocessing.pool.Pool instead of multiprocessing.Pool # because the latter is only a wrapper function, not a proper class. class MyPool(multiprocessing.pool.Pool): Process = NoDaemonProcess def sleepawhile(t): print("Sleeping %i seconds..." % t) time.sleep(t) return t def work(num_procs): print("Creating %i (daemon) workers and jobs in child." % num_procs) pool = multiprocessing.Pool(num_procs) result = pool.map(sleepawhile, [randint(1, 5) for x in range(num_procs)]) # The following is not really needed, since the (daemon) workers of the # child's pool are killed when the child is terminated, but it's good # practice to cleanup after ourselves anyway. pool.close() pool.join() return result def test(): print("Creating 5 (non-daemon) workers and jobs in main process.") pool = MyPool(5) result = pool.map(work, [randint(1, 5) for x in range(5)]) pool.close() pool.join() print(result) if __name__ == '__main__': test() </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload