开发者

Problem with multiprocessing python 2.6

I'm tr开发者_开发问答ying to build a simple program that will fire off a load of processes, and if the main process is killed, the sub processes will die. My code looks like this:

import time
def test_proc(name, conn):
    x = 0
    while True:
        print x
        x += 1
        conn.poll()



from multiprocessing import Process, Pipe

proc_name= ['a', 'b', 'c']
procs = []
for p in proc_name:
    parent_conn, child_conn = Pipe()
    p = Process(target=test_proc, args=(p, child_conn))
    procs.append(p)
    p.start()

while True:
    print [(p.is_alive(), 'Pid %s' %(p.pid)) for p in procs]
    time.sleep(1)

It works, but if I remove the print x on line 5 it doesn't. The processes will continue to run, why?

Also, I'd love to know if this is the right way of doing what I'm trying to achieve.


This works fine for me in Ubuntu:

>>> from time import sleep
>>> from multiprocessing import Process, Pipe
>>> 
>>> def test_proc(name, conn):
...     x = 0
...     while True:
...             #print x
...             x += 1
...             conn.poll()
... 
>>> def main():
...     proc_name= ['a', 'b', 'c']
...     procs = [Process(target=test_proc, args=Pipe()) for p in proc_name]
...     for p in procs:
...             p.start()
...     while True:
...             print [(p.is_alive(), 'Pid %s' %(p.pid)) for p in procs]
...             sleep(1)
... 
>>> main()
[(True, 'Pid 423'), (True, 'Pid 424'), (True, 'Pid 425')]
[(True, 'Pid 423'), (True, 'Pid 424'), (True, 'Pid 425')]
[(True, 'Pid 423'), (True, 'Pid 424'), (True, 'Pid 425')]
[(True, 'Pid 423'), (True, 'Pid 424'), (True, 'Pid 425')]
...

Are you using Windows, maybe? There are programming guidelines that relate to using multiprocessing with Windows. In particular, you need to provide an entry point by using if __name__ == '__main__':.


Later: actually, there is something I don't get. In your original code, you expected to kill the parent of the threads and have the threads keep running. How were you killing the parent -- main() in my code? And if the threads were performing no I/O, how did you know that the threads were still alive?


And later still: When I run the threads, I get this:

>>> main()
[(True, 'Pid 940'), (True, 'Pid 941'), (True, 'Pid 942')]
[(True, 'Pid 940'), (True, 'Pid 941'), (True, 'Pid 942')]
[(True, 'Pid 940'), (True, 'Pid 941'), (True, 'Pid 942')]
[(True, 'Pid 940'), (True, 'Pid 941'), (True, 'Pid 942')]
[(True, 'Pid 940'), (True, 'Pid 941'), (True, 'Pid 942')]

and this:

  PID TTY          TIME CMD
  911 pts/6    00:00:00 python
  940 pts/6    00:00:29 python
  941 pts/6    00:00:29 python
  942 pts/6    00:00:37 python
  944 pts/5    00:00:00 ps

And when I kill the main thread in python (Ctrl-C), I get this:

  PID TTY          TIME CMD
  911 pts/6    00:00:00 python
  940 pts/6    00:00:42 python <defunct>
  941 pts/6    00:00:50 python <defunct>
  942 pts/6    00:00:51 python <defunct>
  946 pts/5    00:00:00 ps

Is this unexpected or undesirable?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜