2

我有一个多处理用例,我想编写一个分派函数,它接收一个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()
4

1 回答 1

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
于 2012-08-08T21:59:53.553 回答