Why does this python queue code process elements multiple times?

Below is the test file I created. Why does each process print a number from 1 to 5 and numbers not separated over the processes?

the code:

#!/usr/bin/python
from subprocess import *

from Queue import Queue
from Queue import Empty

import multiprocessing
from multiprocessing import Process

def main():
    r = Runner()
    r.run()

class Runner(object):
    processes = []

    def run(self):
        q = Queue()
        for t in range(1,6):
            q.put(t)

        for pi in range(1,4):
            p = Process(target=self.runFromQueue, args=(q,))
            p.start()
            self.processes.append(p)

        for p in self.processes:
            p.join()

        print "Finished!"

    def runFromQueue(self, q):
        try:
            while True:
                number = q.get_nowait()
                print str(number)
                q.task_done()

        except Empty:
            pass


if __name__ == "__main__":
    main()

Ouput:

$ ./test_threads.py 
1
2
3
4
5
1
1
2
3
2
4
3
5
4
5
Finished!

Expected Conclusion:

$ ./test_threads.py 
1
2
3
4
5
Finished!
+3
source share
1 answer

The Queue package is not processed by the process; it only works for threads. In your example, the following:

  • Create a queue and fill in with numbers
  • Fork Process 4. This copies the contents of the memory to each subprocess, including the full queue.
  • Each process empties its copy of the queue.

You must use the Queue class provided by multiprocessing.

+8
source

Source: https://habr.com/ru/post/1735913/


All Articles