我有一个用于多处理的用例,我想编写一个调度函数,它接收一个python函数和一个输入队列作为输入,然后通过多处理在该输入上调度该函数?
我不确定args
是否适合传递,因为该函数不会被严格分享。
import multiprocessing
def dispatch(queue, function):
while True:
if queue.empty():
return
current_project = queue.get()
function(current_project)
def letter_rip(projects_filename, function):
project_file = open(projects_filename, 'r')
projects = project_file.readlines()
project_file.close()
q = multiprocessing.Queue()
for project in projects:
q.put(project)
for i in xrange(0, 128):
p = multiprocessing.Process(target=dispatch, args=(q, ???function??? ))
p.start()
答案 0 :(得分:3)
函数是(当用作函数而不是对象时)不可变对象,所以不应该从它们的普通用法中提取问题:
from multiprocessing import Process, Value
def callback(x):
return x * 2
def handler(x, fn, a):
a.value = fn(x.value)
if __name__ == '__main__':
x = Value('d', 2)
a = Value('d', 0)
p1 = Process(target=handler, args=(x, callback, a))
p2 = Process(target=handler, args=(x, callback, a))
p1.start()
p2.start()
p1.join()
p2.join()
print a.value