【问题标题】:Python multiprocessing TypeError: join() takes exactly 1 argument (2 given)Python 多处理 TypeError:join() 只需要 1 个参数(给定 2 个)
【发布时间】:2019-02-10 07:39:26
【问题描述】:

我对多处理进行了很多研究! 基本上我是从 API 下载数据并插入数据库。

我创建了一个池并使用 pool.imap 访问下载功能,将结果创建一个元组并一次性插入数据库中。

我反复访问此功能,有时我的进程会挂起! 我尝试关注https://docs.python.org/2/library/multiprocessing.html#multiprocessing.pool.multiprocessing.Pool.map 并使用超时访问加入。

但是 pool.join(timeout) 返回“TypeError: join() 只需要 1 个参数(给定 2 个)”。我想一个参数是默认的“self”?

一小段代码:

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

我看到了创建进程和生成终止()或加入(超时)的不同方法,但两者都没有使用 imap/map - 这在我的情况下更简单!

【问题讨论】:

    标签: python multithreading multiprocessing terminate


    【解决方案1】:

    这就是解决方案!

    我没有设法使用“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()
    

    谢谢,希望对你有用!

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 哦该死的,所以如果其中一名工人挂了电话,就没有办法通过多进程功能了吗? :(
      • 可以根据文档docs.python.org/2/library/…imap方法中添加超时参数
      • 我将尝试将 x for x in pool.imap(multi_details,items) if x is not None 更改为 for x in pool.imap(multi_details,items): if x.next(1) is not None: in_tuple.append(x) 。如果它有效,我相信会对很多人有用!
      • 我尝试了“下一个”方法,但只收到 8 个响应。 results = pool.imap(multi_details,items) for result in results.next(): print result 我的池类定义 Pool(10) 。我什至尝试创建一个不同的进程monitor = multiprocessing.Process(name='monitor',target=timeout,args=(pool,)),其中 pool arg 是 Pool 对象。里面是“pool.terminate()”,但是没有效果!
      猜你喜欢
      • 2012-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-04
      • 2017-09-15
      • 2018-08-20
      • 1970-01-01
      相关资源
      最近更新 更多