我正在使用
futures.ThreadPoolExecutor()
异步执行网络套接字调用。
我的函数接收消息列表,通过网络套接字发送它们,等待每个响应并返回响应列表。
def batch(self, messages: List[Dict]) -> List:
response = []
with futures.ThreadPoolExecutor() as executor:
future_to_msg = {executor.submit(send, msg): msg for msg in messages}
for future in futures.as_completed(future_to_msg):
m = future_to_msg[future]
try:
resp = future.result()
except Exception as exc:
print(m)
response.append(str(exc))
else:
response.append(resp)
return response
而
send(message: str)
基本上通过网络套接字发送消息并使用SettableEvent()
等待响应
这在大多数时候都很好用。
但有时它会失败,
[WinError 10014] The system detected an invalid pointer address in attempting to use a pointer argument in a call
后跟<WebSocketConnectionError message=socket is already closed.>
我尝试在
max_workers
中使用较少数量的 ThreadPoolExecutor()
来限制资源使用,但行为是相同的。
请告诉我失败时可能出现的问题以及我的实施是否有任何问题。
编辑:我用 2k 条消息列表运行它