[Tutor] running multiple concurrent processes

eryksun eryksun at gmail.com
Sat Nov 3 23:29:00 CET 2012


On Sat, Nov 3, 2012 at 5:59 PM, Oscar Benjamin
<oscar.j.benjamin at gmail.com> wrote:
>
> how_many_spams = Value('i', 1)
>
> p1 = Process(target=update_brain)
> p2 = Process(target=chat_with_friends)
>
> p1.start()
> p2.start()

In Linux you can easily inherit the global Value object in forked
processes, but it's not that hard to support Windows, too:

    if __name__ == '__main__':
        how_many_spams = Value('i', 1)
        args = (how_many_spams,)
        p1 = Process(target=update_brain, args=args)
        p2 = Process(target=chat_with_friends, args=args)
        p1.start()
        p2.start()

On Windows, multiprocessing has to launch a fresh interpreter, import
the main module, and pickle/pipe the args to the new process. Setup
code that should only run in the main process has to be guarded with a
__name__ check.


More information about the Tutor mailing list