在windows上多处理python的最佳方法

在windows上多处理python的最佳方法,python,windows,multiprocessing,Python,Windows,Multiprocessing,我必须做一个程序,读取PDF,将每个页面转换为PNG文件,然后为每个图像的每个页面并行运行一些代码。我正在寻找创建一个父进程的方法,其中包含恒定数量的子进程 父亲必须把工作交给孩子们。如果父亲有21页要处理,并且只有5个子项,那么父亲必须管理一个队列来发送工作,而不会杀死5个子项并创建新的子项。当孩子完成他的工作时,他给父亲发了一条信息,让他做新的工作 我不想杀死子进程,因为我认为这比杀死并创建新的子进程或子进程要快。我走错方向了 我正试图使用multiprocessing.apply\u as

我必须做一个程序,读取PDF,将每个页面转换为PNG文件,然后为每个图像的每个页面并行运行一些代码。我正在寻找创建一个父进程的方法,其中包含恒定数量的子进程

父亲必须把工作交给孩子们。如果父亲有21页要处理,并且只有5个子项,那么父亲必须管理一个队列来发送工作,而不会杀死5个子项并创建新的子项。当孩子完成他的工作时,他给父亲发了一条信息,让他做新的工作

我不想杀死子进程,因为我认为这比杀死并创建新的子进程或子进程要快。我走错方向了

我正试图使用multiprocessing.apply\u async来实现这一点,但我没有找到实现所需的方法

一些建议或指导

对不起,我的英语很差

我正在尝试做的一些代码:

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()

你现在的工作方式应该很好<代码>多处理。池创建一个具有固定数量工作进程的池,所有工作进程都将在池的生命周期内保持活动状态。
有一个内部队列,用于在其中一个工作进程完成工作后立即将工作项发送到工作进程。因此,您所需要做的就是将所有需要完成的工作输入
,然后
将处理所有这些工作分配给您的三个工作进程

以您的示例为例,除了现在,我们为其提供了30个工作项:

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()
以下是输出:

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
进程:处理0
流程:处理2
流程:处理3
流程:处理4
流程:处理5
0
流程:处理1
9
4.
流程:处理6
流程:处理7
16
流程:处理8
25
1.
流程:处理9
36
49
64
81
流程:处理10
100
流程:处理11
流程:处理12
121
144
流程:处理13
流程:处理14
169
流程:处理15
196
流程:处理16
225
流程:处理17
256
流程:处理18
流程:处理19
流程:处理20
289
流程:处理21
324
流程:处理22
361
流程:处理24
400
流程:处理25
441
流程:处理26
流程:处理27
484
576
流程:处理28
流程:处理29
625
676
729
784
841
流程:处理23
529

正如您所看到的,工作在您的员工之间分配,您不必做任何特殊的事情。

向我们展示一些代码。也许你只是缺少了一点代码。我只是添加了一些代码,但我不知道如何与父亲沟通,告诉他孩子已经完成了他的工作,并要求更多的工作。好的,你可以使用多处理队列,检查队列中是否还有项。这正是我需要的!谢谢!