从后台的python生成器中,从单独的进程中预取元素
multiprocessing-generator的Python项目详细描述
从后台的python生成器中预取项目的库, 使用单独的过程。
安装(无依赖项):
pip install multiprocessing_generator
示例:
from multiprocessing_generator import ParallelGenerator def my_generator(): while True: # ... download something long ... yield result with ParallelGenerator( my_generator(), max_lookahead=100) as g: for elem in g: # ... do some heavy processing on that element ...
在消耗的元素之前,最多有100个元素将由 在后台生成程序,这在生成程序和 使用者不使用相同的资源(例如网络与CPU)。
生成器处理异常和更严重的故障 透明的。
在麻省理工学院的许可下发布。
另请参见:https://github.com/justheuristic/prefetch_generator