我已经进行了很多有关多处理的研究! 基本上,我是从API下载数据并插入数据库中。
我创建了一个池,并使用pool.imap访问下载功能,将结果与元组并一并插入到数据库中。
我反复访问此功能,有时我的过程挂起! 我尝试遵循https://docs.python.org/2/library/multiprocessing.html#multiprocessing.pool.multiprocessing.Pool.map并使用超时访问联接。
但是pool.join(timeout)返回“ TypeError:join()恰好接受1个参数(给定2个)”。我想一个参数是默认的“自我”吗?
一小段代码:
timeout = 10
pool = Pool(10)
in_tuple = [x for x in pool.imap(multi_details,items) if x is not None]
pool.close()
pool.join(timeout) # from the documentation I should be able to put the timeout in join
writing_to_database(in_tuple)
# function that generate the content for DB
def multi_details(item):
tuple = get_details(item)
return tuple
我看到了不同的方法来创建进程并生成terminate()或join(timeout),但是没人使用imap / map-在我的情况下,这要简单得多!
答案 0 :(得分:1)
与Process
类不同,Pool
类在其timeout
方法中不接受join
参数:
https://docs.python.org/2/library/multiprocessing.html#multiprocessing.pool.multiprocessing.Pool.join
答案 1 :(得分:0)
这是解决方案!
我没有使用“ next(timeout)”,因为它只是在运行整个列表之前解析了几项而不是停止!
我开始使用 apply_async 。唯一的是,我有一种奇怪的感觉,就是它比 imap 慢。
功能代码为:
grep
谢谢,我希望它会有用!