Python多处理TypeError:join()只取1个参数(给定2个)

问题描述 投票:1回答:2

我做了很多关于多处理的研究!基本上我是从API下载数据并插入数据库。

我创建了一个池并使用pool.imap访问下载功能,使用结果创建一个元组并在DB中一次性插入所有内容。

我反复访问这个函数,在某些时候我的进程挂起了!我试图关注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(超时)的不同方法,但是没有人使用imap / map - 这在我的情况下更简单!

python multithreading multiprocessing terminate
2个回答
1
投票

Process类不同,Pool类在其timeout方法中不接受join参数:https://docs.python.org/2/library/multiprocessing.html#multiprocessing.pool.multiprocessing.Pool.join


0
投票

这是解决方案!

我没有设法使用“next(timeout)”因为它只是在运行整个列表之前解析了几个项目而不是停止!

我开始使用apply_async。唯一的问题是我有一种奇怪的感觉,它比imap慢。

功能代码是:

timeout = 1
pool = Pool(10)
for x in items:
    try:
        res = pool.apply_async(multi_details,(x,)).get(timeout)
    except Exception as e:
        pass # you can put anything you want but my scope was to skip the things that took too much!
    else:
        if res is not None: # now this could be a better pythonic way to write this. Any help will be highly appreciated!
            in_tuple.append(res)
pool.close()
pool.join()

谢谢,我希望它有用!

© www.soinside.com 2019 - 2024. All rights reserved.