我试图在http://docs.python.org/dev/library/multiprocessing.html阅读文档,但我仍然在努力处理多处理队列,池和锁定。现在我能够构建下面的例子。
关于队列和游泳池,我不确定我是否以正确的方式理解这个概念,所以如果我错了,请纠正我。我想要达到的目标是 处理2个请求(数据列表在本例中有8个)所以,我应该使用什么?池创建2个进程,可以处理两个不同的队列(最多2个)或者我应该只使用Queue每次处理2个输入?锁定将是正确打印输出。
import multiprocessing
import time
data = (['a', '2'], ['b', '4'], ['c', '6'], ['d', '8'],
['e', '1'], ['f', '3'], ['g', '5'], ['h', '7']
)
def mp_handler(var1):
for indata in var1:
p = multiprocessing.Process(target=mp_worker, args=(indata[0], indata[1]))
p.start()
def mp_worker(inputs, the_time):
print " Processs %s\tWaiting %s seconds" % (inputs, the_time)
time.sleep(int(the_time))
print " Process %s\tDONE" % inputs
if __name__ == '__main__':
mp_handler(data)
答案 0 :(得分:110)
针对您的问题的最佳解决方案是使用Pool
。使用Queue
并具有单独的“队列馈送”功能可能有点过分。
这是您的程序稍微重新排列的版本,这次只有{strong>仅有2个进程在Pool
中。我相信这是最简单的方法,对原始代码的改动很小:
import multiprocessing
import time
data = (
['a', '2'], ['b', '4'], ['c', '6'], ['d', '8'],
['e', '1'], ['f', '3'], ['g', '5'], ['h', '7']
)
def mp_worker((inputs, the_time)):
print " Processs %s\tWaiting %s seconds" % (inputs, the_time)
time.sleep(int(the_time))
print " Process %s\tDONE" % inputs
def mp_handler():
p = multiprocessing.Pool(2)
p.map(mp_worker, data)
if __name__ == '__main__':
mp_handler()
请注意,mp_worker()
函数现在接受单个参数(前两个参数的元组),因为map()
函数将输入数据分块到子列表中,每个子列表作为单个参数提供给您工人职能。
输出:
Processs a Waiting 2 seconds
Processs b Waiting 4 seconds
Process a DONE
Processs c Waiting 6 seconds
Process b DONE
Processs d Waiting 8 seconds
Process c DONE
Processs e Waiting 1 seconds
Process e DONE
Processs f Waiting 3 seconds
Process d DONE
Processs g Waiting 5 seconds
Process f DONE
Processs h Waiting 7 seconds
Process g DONE
Process h DONE
根据以下@Thales评论进行编辑:
如果您希望“锁定每个池限制”,以便您的进程以串联方式运行,则ala:
等待B等待|完成,B完成| C等待,D等待| C完成,D完成| ...然后将处理程序函数更改为为每对数据启动池(2个进程):
def mp_handler():
subdata = zip(data[0::2], data[1::2])
for task1, task2 in subdata:
p = multiprocessing.Pool(2)
p.map(mp_worker, (task1, task2))
现在你的输出是:
Processs a Waiting 2 seconds
Processs b Waiting 4 seconds
Process a DONE
Process b DONE
Processs c Waiting 6 seconds
Processs d Waiting 8 seconds
Process c DONE
Process d DONE
Processs e Waiting 1 seconds
Processs f Waiting 3 seconds
Process e DONE
Process f DONE
Processs g Waiting 5 seconds
Processs h Waiting 7 seconds
Process g DONE
Process h DONE
答案 1 :(得分:8)
以下是我对此主题的个人转到:
这里的要点,(拉请求欢迎!): https://gist.github.com/thorsummoner/b5b1dfcff7e7fdd334ec
import multiprocessing
import sys
THREADS = 3
# Used to prevent multiple threads from mixing thier output
GLOBALLOCK = multiprocessing.Lock()
def func_worker(args):
"""This function will be called by each thread.
This function can not be a class method.
"""
# Expand list of args into named args.
str1, str2 = args
del args
# Work
# ...
# Serial-only Portion
GLOBALLOCK.acquire()
print(str1)
print(str2)
GLOBALLOCK.release()
def main(argp=None):
"""Multiprocessing Spawn Example
"""
# Create the number of threads you want
pool = multiprocessing.Pool(THREADS)
# Define two jobs, each with two args.
func_args = [
('Hello', 'World',),
('Goodbye', 'World',),
]
try:
# Spawn up to 9999999 jobs, I think this is the maximum possible.
# I do not know what happens if you exceed this.
pool.map_async(func_worker, func_args).get(9999999)
except KeyboardInterrupt:
# Allow ^C to interrupt from any thread.
sys.stdout.write('\033[0m')
sys.stdout.write('User Interupt\n')
pool.close()
if __name__ == '__main__':
main()
答案 2 :(得分:7)
这可能与问题没有100%的关联,但在我搜索使用队列多处理的示例时,这首先在google上显示。
这是一个基本的示例类,您可以实例化并将项目放入队列中,并且可以等到队列完成。这就是我所需要的一切。
from multiprocessing import JoinableQueue
from multiprocessing.context import Process
class Renderer:
queue = None
def __init__(self, nb_workers=2):
self.queue = JoinableQueue()
self.processes = [Process(target=self.upload) for i in range(nb_workers)]
for p in self.processes:
p.start()
def render(self, item):
self.queue.put(item)
def upload(self):
while True:
item = self.queue.get()
if item is None:
break
# process your item here
self.queue.task_done()
def terminate(self):
""" wait until queue is empty and terminate processes """
self.queue.join()
for p in self.processes:
p.terminate()
r = Renderer()
r.render(item1)
r.render(item2)
r.terminate()
答案 3 :(得分:1)
对于使用Komodo Edit(win10)等编辑器的所有人,请将sys.stdout.flush()
添加到:
def mp_worker((inputs, the_time)):
print " Process %s\tWaiting %s seconds" % (inputs, the_time)
time.sleep(int(the_time))
print " Process %s\tDONE" % inputs
sys.stdout.flush()
或作为第一行:
if __name__ == '__main__':
sys.stdout.flush()
这有助于了解脚本运行期间发生了什么;而不必查看黑色命令行框。
答案 4 :(得分:0)
以下是我的代码中的示例(对于线程池,但只是更改类名,您将拥有进程池):
def execute_run(rp):
... do something
pool = ThreadPoolExecutor(6)
for mat in TESTED_MATERIAL:
for en in TESTED_ENERGIES:
for ecut in TESTED_E_CUT:
rp = RunParams(
simulations, DEST_DIR,
PARTICLE, mat, 960, 0.125, ecut, en
)
pool.submit(execute_run, rp)
pool.join()
基本上:
pool = ThreadPoolExecutor(6)
为6个帖子创建一个池pool.submit(execute_run, rp)
向池添加任务,第一个arogument是在线程/进程中调用的函数,其余参数传递给被调用函数。 pool.join
等待所有任务完成。