【发布时间】: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