Create a new process to run python function

James Mills prologic at shortcircuit.net.au
Wed May 5 10:33:29 EDT 2010


On Wed, May 5, 2010 at 10:56 PM, Massi <massi_srb at msn.com> wrote:
> in my script (python 2.5 on windows xp) I need to run a simple
> function in a separate process. In other words I need something
> similar to the fork function under UNIX. I tried with threads:

Use the new multiprocesing package.

> import os, threading
>
> def func(s) :
>    print "I'm thread number "+s, os.getpid()
>
> threading.Thread(target=func, args=("1",)).start()
> threading.Thread(target=func, args=("2",)).start()

Like this:

import multiprocessing

multiprocessing.Process(target=func, args=("1",)).start()
multiprocessing.Process(target=func, args=("2",)).start()

...

Surprise surprise it has almost the same API
as the threading module :)

--James



More information about the Python-list mailing list