我有一個帶有某種main-loop的服務。在此服務中,我需要使用一些有關操作的元資料向其他服務發送請求。我需要在不阻塞主執行緒的情況下異步進行。
這是我的代碼:
def process_response(self, request, response, spider):
if self.enabled:
Thread(target=self._send_request_to_information_supplier,
args=(response,)).start()
# I don't want this flow to be blocked by above thread.
return response
return response
def _send_request_to_information_supplier(self, html_response):
some_metadata = InformationSupplierMetadata(url=html_response.url,
html=html_response.text).convert_to_dict()
try:
request_response = requests.post(url=self.url,
data=some_metadata,
headers={'Content-Type': 'application/json; charset=utf-8'},
timeout=self.timeout,
verify=False)
self.logger.debug('Request was sent and end up with status {0}'.format(request_response.status_code))
except ConnectTimeout:
self.logger.debug('Request timed out')
我是多執行緒的初學者,我不確定這是正確的方法。也許有人可以提出更好的方法?
uj5u.com熱心網友回復:
你可以使用ThreadPoolExecutor
. 這是一個可能會有所幫助的示例。
from concurrent.futures import ThreadPoolExecutor
executor = ThreadPoolExecutor(24)
def process_response(self, request, response, spider):
if self.enabled:
executor.submit(self._send_request_to_information_supplier, response)
# I don't want this flow to be blocked by above thread.
return response
return response
def _send_request_to_information_supplier(self, html_response):
...
這樣,執行程式將擁有 24 個最大執行緒數,并且提交將在一個執行緒中運行該方法(如果有的話)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/508493.html
上一篇:異步Dart功能無法正常作業