Multiprocessing / threading confusion

Piet van Oostrum piet at vanoostrum.org
Fri Sep 6 17:15:56 EDT 2013


Paul Pittlerson <menkomigen6 at gmail.com> writes:

[...]
>     def run(self):
>         while True:
>             
>             sleep(0.1)
>             
>             if not self.q.empty():
>                 print self.q.get()
>                 
>             else:
>                 break
[...]

> This works great on linux, but does not run on windows (7). The behavior was: I 
> opened it with double clicking and so a window appeared and disappeared (no 
> text) then I opened it with IDLE and ran it there, where it worked a couple 
> times. Then reopened it with IDLE and this time it did not work at all. After 
> that the script did not run either through IDLE or opening directly.
>
> What may be the reason it works on linux, but seems buggy on windows?

That it works on Linux is just coincidence. Your script is still timing
dependent because the while loop in Debug.run stops when the queue is
empty. As has been explained in other answers, the queue can just become
empty when Debug empties it faster than the other processes can fill it.
That is entirely dependent on the scheduling of the O.S. so you have no
control over it. You must use a safe way to stop, for example to count
the exited messages.

Another way is to join all the processes in the main program, and after
that put a special END message to the queue, which causes Debug to stop:

class Debugger(Thread):
...        
    def run(self):
        while True:
            sleep(0.1)
            msg = self.q.get()
            print(msg)
            if 'END' in msg:
                break

..main..
    processes = []
    for i in range(5):
        
        d = Process(target=Worker, args=(debug_q,))
        d.start()
        processes.append(d)

    for p in processes:
        p.join()
    debug_q.put('END')

-- 
Piet van Oostrum <piet at vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]



More information about the Python-list mailing list