使用futures.ThreadPoolExecutor异步执行web socket调用时WinError 10014

问题描述 投票:0回答:0

我正在使用

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 条消息列表运行它

python-3.x websocket concurrent.futures
© www.soinside.com 2019 - 2024. All rights reserved.