Best way to multiprocessing python on windows

flaviussn

I have to do a program that reads a PDF, convert each page to PNG file, and then run some code in parallel for each page of each image. I'm searching the way to create a father process, with a constant number of children processes.

The father process have to send work to the children. If the father has 21 pages to process, and has only 5 children, the father has to manage a queue to send work without kill the 5 children and create new children. When the child finish his work, he send a message to the father to send him new work.

I don't want kill the children because I think is faster than kill and create new subprocesses or children. I'm in the wrong direction ?

I'm trying to do this using multiprocessing.apply_async, but I don't found the way to do what I need.

Some advice or tutorial ?

Sorry for my poor english

Some code what I'm trying to do:

from multiprocessing  import Pool
import time
import random

def BarcodeSearcher(x):
    #Here goes the image processing    
    return x*x 

def resultCollector(result):
    print result

def main():
    pool = Pool(processes=3)
    for pag in range(3):
        pool.apply_async(BarcodeSearcher, args = (pag, ), callback = resultCollector) 
    pool.close()
    pool.join()

if __name__ == '__main__':
    main()
dano

The way you're currently doing it should work just fine. multiprocessing.Pool creates a pool with a constant number of worker processes, all of which will stay alive for the lifetime of the Pool. The Pool has an internal queue that is used to send work items to the worker processes as soon as one of them finishes doing work. So, all you need to do is feed all the work you want done into the Pool, and then the Pool will handle distributing it all to your three worker processes.

Consider your example, except now we're feeding it 30 work items:

from multiprocessing import Pool, current_process
import time
import random

def BarcodeSearcher(x):
    print ("Process %s: handling %s" % (current_process(), x)
    #Here goes the image processing    
    return x*x 

def resultCollector(result):
    print result

def main():
    pool = Pool(processes=3)
    for pag in range(30):
        pool.apply_async(BarcodeSearcher, args = (pag, ), callback = resultCollector) 
    pool.close()
    pool.join()

if __name__ == '__main__':
    main()

Here's the output:

Process <Process(PoolWorker-1, started daemon)>: handling 0
Process <Process(PoolWorker-3, started daemon)>: handling 2
Process <Process(PoolWorker-1, started daemon)>: handling 3
Process <Process(PoolWorker-1, started daemon)>: handling 4
Process <Process(PoolWorker-3, started daemon)>: handling 5
0
Process <Process(PoolWorker-2, started daemon)>: handling 1
9
4
Process <Process(PoolWorker-1, started daemon)>: handling 6
Process <Process(PoolWorker-1, started daemon)>: handling 7
16
Process <Process(PoolWorker-2, started daemon)>: handling 8
25
1
Process <Process(PoolWorker-3, started daemon)>: handling 9
36
49
64
81
Process <Process(PoolWorker-1, started daemon)>: handling 10
100
Process <Process(PoolWorker-2, started daemon)>: handling 11
Process <Process(PoolWorker-3, started daemon)>: handling 12
121
144
Process <Process(PoolWorker-2, started daemon)>: handling 13
Process <Process(PoolWorker-1, started daemon)>: handling 14
169
Process <Process(PoolWorker-2, started daemon)>: handling 15
196
Process <Process(PoolWorker-1, started daemon)>: handling 16
225
Process <Process(PoolWorker-3, started daemon)>: handling 17
256
Process <Process(PoolWorker-3, started daemon)>: handling 18
Process <Process(PoolWorker-1, started daemon)>: handling 19
Process <Process(PoolWorker-1, started daemon)>: handling 20
289
Process <Process(PoolWorker-1, started daemon)>: handling 21
324
Process <Process(PoolWorker-3, started daemon)>: handling 22
361
Process <Process(PoolWorker-3, started daemon)>: handling 24
400
Process <Process(PoolWorker-1, started daemon)>: handling 25
441
Process <Process(PoolWorker-3, started daemon)>: handling 26
Process <Process(PoolWorker-1, started daemon)>: handling 27
484
576
Process <Process(PoolWorker-3, started daemon)>: handling 28
Process <Process(PoolWorker-1, started daemon)>: handling 29
625
676
729
784
841
Process <Process(PoolWorker-2, started daemon)>: handling 23
529

As you can see, the work got distributed between your workers without you having to do anything special.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python multiprocessing (joblib) best way for argument passing

From Dev

Best way to perform multiprocessing on a large file Python

From Dev

Python, Windows, and Multiprocessing

From Dev

Python Logging with Multiprocessing in Windows

From Java

Python Multiprocessing: efficiently only save the best runs

From Java

RuntimeError on windows trying python multiprocessing

From Dev

RuntimeError on windows trying python multiprocessing

From Dev

Python multiprocessing works in linux but not in windows

From Dev

Better way to share memory for multiprocessing in Python?

From Dev

python multiprocessing on windows, if __name__ == "__main__"

From Dev

How does Python handle multiprocessing on Windows?

From Dev

Python code with multiprocessing does not work on Windows

From Dev

How to do multiprocessing using Python for .NET on Windows?

From Dev

Python multiprocessing Pool strange behavior in Windows

From Dev

Python multiprocessing.Process object with multiprocessing.Manager creates multiple multiprocessing forks in Windows Task Manager

From Dev

Best way to validate a name in Python

From Dev

best way for python to interface with MySQL

From Dev

Best way to "overload" function in python?

From Dev

Best way to validate a name in Python

From Dev

Best way to call a method - Python

From Dev

Best way to quit a python programme?

From Dev

Best way to load plugins on windows that link to executable?

From Dev

Best way to do a task looping in Windows Service

From Dev

Best way for Windows Phone Data and Memory

From Dev

Best way to load plugins on windows that link to executable?

From Dev

Windows phone app with many image - best way?

From Dev

What is the best way to automate Windows at a high level?

From Dev

best way to dual boot Ubuntu with windows 10?

From Dev

Preferred way to empty multiprocessing.queue(-1) in python

Related Related

  1. 1

    Python multiprocessing (joblib) best way for argument passing

  2. 2

    Best way to perform multiprocessing on a large file Python

  3. 3

    Python, Windows, and Multiprocessing

  4. 4

    Python Logging with Multiprocessing in Windows

  5. 5

    Python Multiprocessing: efficiently only save the best runs

  6. 6

    RuntimeError on windows trying python multiprocessing

  7. 7

    RuntimeError on windows trying python multiprocessing

  8. 8

    Python multiprocessing works in linux but not in windows

  9. 9

    Better way to share memory for multiprocessing in Python?

  10. 10

    python multiprocessing on windows, if __name__ == "__main__"

  11. 11

    How does Python handle multiprocessing on Windows?

  12. 12

    Python code with multiprocessing does not work on Windows

  13. 13

    How to do multiprocessing using Python for .NET on Windows?

  14. 14

    Python multiprocessing Pool strange behavior in Windows

  15. 15

    Python multiprocessing.Process object with multiprocessing.Manager creates multiple multiprocessing forks in Windows Task Manager

  16. 16

    Best way to validate a name in Python

  17. 17

    best way for python to interface with MySQL

  18. 18

    Best way to "overload" function in python?

  19. 19

    Best way to validate a name in Python

  20. 20

    Best way to call a method - Python

  21. 21

    Best way to quit a python programme?

  22. 22

    Best way to load plugins on windows that link to executable?

  23. 23

    Best way to do a task looping in Windows Service

  24. 24

    Best way for Windows Phone Data and Memory

  25. 25

    Best way to load plugins on windows that link to executable?

  26. 26

    Windows phone app with many image - best way?

  27. 27

    What is the best way to automate Windows at a high level?

  28. 28

    best way to dual boot Ubuntu with windows 10?

  29. 29

    Preferred way to empty multiprocessing.queue(-1) in python

HotTag

Archive