问题描述
我有一个列表 H = [item1, item2, item3....so on]
和一个函数,
def start(item1):p1 = 对 item1 做点什么返回 p1
我希望函数 start 应该为列表 H 中的每个项目并行运行.我知道多处理,但我已经有 4 个使用多处理并行运行的列表.而如何实现列表中每个项目的线程化?有人可以用示例代码解释一下吗.
谢谢!
制作一个在线程中运行给定函数并存储结果的函数:
导入线程def run_item(f, item):result_info = [threading.Event(), None]定义运行():result_info[1] = f(item)result_info[0].set()threading.Thread(target=runit).start()返回 result_info
然后是另一个收集结果的函数:
def gather_results(result_infos):结果 = []对于 xrange(len(result_infos)) 中的 i:result_infos[i][0].wait()结果.追加(result_infos[i][1])返回结果
然后从主线程说 proc
是处理项目的函数,items
是要处理的项目列表:
#开始处理物品result_infos = [run_item(proc, item) for item in items]#收集结果(阻塞)结果=gather_results(result_infos)
示例用法:
>>>导入时间>>>def proc(项目):... time.sleep(2.0)...返回项目 * 2...>>>打印gather_results([run_item(proc, item) for item in [1, 2, 10, 100]])#2 秒后...[2, 4, 20, 200]I have a list H = [item1, item2, item3....so on]
and a function,
def start(item1):
p1 = Do something to item1
return p1
I want the function start should run in parallel for each item in list H. I know multiprocessing, but I already have 4 list that runs in parallel using multiprocessing. While how the threading for each item in a list could be achieved? Can somebody please explain this with example code.
Thanks!
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]
这篇关于在 Python 中为列表中的每个项目线程化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!