我在加速 Pywikibot 方面遇到了问题。我在 StackOverflow 上看到了相关问题,但它们仅部分适用于我的问题:
- 我
throttle=False
尽可能地设置,但机器人仍然很慢。 我不能使用此处
PreloadingPageGenerator
建议的类似内容,因为我不是使用 Bot 来访问 Wikipedia 而是 Wikidata。就我而言,请求看起来像这样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()
但是在我运行程序的那一刻,它甚至没有完成第一个请求,因为它被卡住了。我跟着这个错误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()
to pywikibot/comms/threadedhttp.py:359 _join()
,获得的锁似乎永远不会被释放
def _join(self):
"""Block until response has arrived."""
self.lock.acquire(True)
我现在的问题是:这是一个错误pywikibot
吗?我是否multiprocessing
以错误的方式应用了这个问题?在我的具体情况下是否有其他解决方案可以加快速度pywikibot
?