Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80696

Re: Help with Python Multiprocessing

Date 2014-11-13 18:45 +0000
From MRAB <python@mrabarnett.plus.com>
Subject Re: Help with Python Multiprocessing
References <91fd5810-6a2a-46d8-8d1e-b68afced3987@googlegroups.com> <b8de6dad-de52-42d6-b5aa-1485c15cc794@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.15784.1415904344.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 2014-11-13 18:10, Anurag wrote:
> On Thursday, November 13, 2014 1:07:56 PM UTC-5, Anurag wrote:
>> I am having trouble understanding the Multiprocessing module.
>> I need to run three different files 'Worker1' , 'Worker2', 'Worker3' all at once. Currently I am doing this :
>>
>> from multiprocessing import Process
>>
>> import Worker1.py
>> import Worker2.py
>> import Worker3.py
>>
>>
>>
>> p1 = Process(target=Worker1.py)
>> p1.start()
>> p2 = Process(target=Worker2.py)
>> p2.start()
>> p3 = Process(target=Worker3.py)
>> p3.start()
>>
>> But this will only start the 'Worker1'. How do I execute all the three files at once?
>>
>> Thanks
>
> I am using Python 2.7 and each of my 'Worker' has a never ending loop that runs continuously.
>
The documentation has this example:


from multiprocessing import Process

def f(name):
     print 'hello', name

if __name__ == '__main__':
     p = Process(target=f, args=('bob',))
     p.start()
     p.join()


Suppose this is in a module called 'example.py'.

When 'example.py' is run as the main module, it executes:

     p = Process(target=f, args=('bob',))
     p.start()

The multiprocessing module will _import_ 'example.py' in a new process,
and because it's only imported (and not run as a main module), that
code won't be executed. The multiprocessing module will then call the
'f' function.

The way you've written your module, it'll spawn a new process even when
it's only imported, causing the spawned process to spawn another
process, which will, in turn, spawn another...

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Help with Python Multiprocessing Anurag <anuragpatibandla7@gmail.com> - 2014-11-13 10:07 -0800
  Re: Help with Python Multiprocessing Anurag <anuragpatibandla7@gmail.com> - 2014-11-13 10:10 -0800
    Re: Help with Python Multiprocessing MRAB <python@mrabarnett.plus.com> - 2014-11-13 18:45 +0000
  Re: Help with Python Multiprocessing sohcahtoa82@gmail.com - 2014-11-13 11:18 -0800
    Re: Help with Python Multiprocessing Anurag <anuragpatibandla7@gmail.com> - 2014-11-13 15:22 -0800
      Re: Help with Python Multiprocessing sohcahtoa82@gmail.com - 2014-11-14 11:41 -0800
  Re: Help with Python Multiprocessing Gary Herron <gary.herron@islandtraining.com> - 2014-11-13 11:12 -0800
    Re: Help with Python Multiprocessing Anurag <anuragpatibandla7@gmail.com> - 2014-11-13 15:21 -0800
  Re: Help with Python Multiprocessing Anurag <anuragpatibandla7@gmail.com> - 2014-11-23 12:56 -0800
    Re: Help with Python Multiprocessing sohcahtoa82@gmail.com - 2014-11-24 12:58 -0800

csiph-web