【问题标题】:Deadlock while using multiprocessing to speed up Pywikibot with Wikidata?使用多处理加速带有 Wikidata 的 Pywikibot 时出现死锁?
【发布时间】:2017-04-11 11:45:39
【问题描述】:

我在加速 Pywikibot 方面遇到了问题。我在 StackOverflow 上看到过相关问题,但它们仅部分适用于我的问题:

  • 我尽可能设置throttle=False,但机器人仍然很慢。
  • 我不能像建议的here 那样使用PreloadingPageGenerator,因为我不是使用机器人来访问维基百科而是维基数据。就我而言,请求看起来像这样

    from pywikibot.data import api
    
    request_parameters = {
         'action': 'wbsearchentities',
         'format': 'json',
         'language': language,
         'type': 'item',
         'search': name,
         'throttle': False
    }
    request = api.Request(site=self.wikidata_site, use_get=True, **request_parameters)
    response = request.submit()
    

我现在尝试使用multiprocessing,因此可以一次向 API 发送多个请求,这样就无需等待响应才能继续下一个请求,如下所示

while not queue.empty():  # Queue holding data for requests
    job_data = [queue.get() for i in range(number_of_processes)]

    jobs = [
        multiprocessing.Process(
                target=search_for_entity,
                args=(name, language)
            )
        for name, language in job_data
    ]

    for job in jobs:
        job.start()

    for job in jobs:
        job.join()

但是当我运行程序时,它甚至没有完成第一个请求,因为它卡住了。我跟着bug到pywikibot/data/api.py:1500 submit()

 rawdata = http.request(
                site=self.site, uri=uri, method='GET' if use_get else 'POST',
                body=body, headers=headers)

通过pywikibot/comms/http.py:361 fetch():

request = _enqueue(uri, method, body, headers, **kwargs)
request._join()

pywikibot/comms/threadedhttp.py:359 _join(),获得的锁似乎永远不会被释放

def _join(self):
    """Block until response has arrived."""
    self.lock.acquire(True)

我现在的问题是:这是pywikibot 的错误吗?我是否以错误的方式将multiprocessing 应用于此问题?在我的具体情况下是否有任何其他解决方案可以加快pywikibot

【问题讨论】:

    标签: python python-3.x multiprocessing wikidata pywikibot


    【解决方案1】:

    此代码部分非常过时,并且您已经展示了线程 http 模块多年前已被删除。我建议更新你的 Pywikibot。

    【讨论】:

    • 这不是答案。
    • 是的。你不能做任何改进,比如为过时的框架加速。
    猜你喜欢
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 2021-06-09
    • 1970-01-01
    相关资源
    最近更新 更多