1

我有一个列表H = [item1, item2, item3....so on]和一个函数,

def start(item1):
    p1 = Do something to item1     
    return p1

我希望函数 start 应该为列表 H 中的每个项目并行运行。我知道多处理,但我已经有 4 个使用多处理并行运行的列表。而如何实现列表中每个项目的线程化?有人可以用示例代码解释一下吗。

谢谢!

4

1 回答 1

4

Make a function which runs a given function in a thread and stores the result:

import threading
def run_item(f, item):
    result_info = [threading.Event(), None]
    def runit():
        result_info[1] = f(item)
        result_info[0].set()
    threading.Thread(target=runit).start()
    return result_info

Then another function to gather the results:

def gather_results(result_infos):
    results = [] 
    for i in xrange(len(result_infos)):
        result_infos[i][0].wait()
        results.append(result_infos[i][1])
    return results

Then from the main thread, say proc is the function that processes an item and items is your list of items to process:

#start processing the items
result_infos = [run_item(proc, item) for item in items]
#gather the results (blocking)
results = gather_results(result_infos)

Example usage:

>>> import time
>>> def proc(item):
...     time.sleep(2.0)
...     return item * 2
... 
>>> print gather_results([run_item(proc, item) for item in [1, 2, 10, 100]])
#2 seconds later...
[2, 4, 20, 200]
于 2013-10-11T15:46:09.250 回答