Create a new process to run python function
Hi everyone, 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: 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() but this does not work, since the two threads share the same pid. Can anyone give me a suggestion? Thanks in advance. -- http://mail.python.org/mailman/listinfo/python-list
Re: Create a new process to run python function
On Wed, May 5, 2010 at 8:56 AM, Massi massi_...@msn.com wrote: but this does not work, since the two threads share the same pid. Can anyone give me a suggestion? Have you looked at os.fork ? http://docs.python.org/library/os.html#os.fork -- http://mail.python.org/mailman/listinfo/python-list
Re: Create a new process to run python function
Joe Riopel wrote: On Wed, May 5, 2010 at 8:56 AM, Massimassi_...@msn.com wrote: but this does not work, since the two threads share the same pid. Can anyone give me a suggestion? Have you looked at os.fork ? http://docs.python.org/library/os.html#os.fork Fork on Windows XP? Have a lot of fun ... The NT Kernel has support for forking but neither the Win32 API nor Python supports it. Christian -- http://mail.python.org/mailman/listinfo/python-list
Re: Create a new process to run python function
On Wed, May 5, 2010 at 10:56 PM, Massi massi_...@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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Create a new process to run python function
On Wed, May 5, 2010 at 10:33 AM, James Mills prolo...@shortcircuit.net.au wrote: On Wed, May 5, 2010 at 10:56 PM, Massi massi_...@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 Multiprocessing wasn't added until Python 2.6. http://www.python.org/dev/peps/pep-0371/ In Python 2.5, it was still a 3rd party package. http://pypi.python.org/pypi/processing The project's website appears to be down right now though. http://developer.berlios.de/projects/pyprocessing -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Create a new process to run python function
Am 05.05.2010 17:59, schrieb Benjamin Kaplan: Multiprocessing wasn't added until Python 2.6. http://www.python.org/dev/peps/pep-0371/ In Python 2.5, it was still a 3rd party package. http://pypi.python.org/pypi/processing The project's website appears to be down right now though. http://developer.berlios.de/projects/pyprocessing Why don't you try out our backport: http://pypi.python.org/pypi/multiprocessing/2.6.2.1 :) -- http://mail.python.org/mailman/listinfo/python-list