Python 3.x 客户端从aiohttp断开连接错误

Python 3.x 客户端从aiohttp断开连接错误,python-3.x,websocket,python-3.4,python-asyncio,aiohttp,Python 3.x,Websocket,Python 3.4,Python Asyncio,Aiohttp,我有一个用aiohttp构建的websocket服务器。 我一直在服务器错误流中获取此异常 Task exception was never retrieved future: <Task finished coro=<read() done, defined at /usr/local/lib/python3.4/dist-packages/aiohttp/streams.py:576> exception=ClientDisconnectedError()> Trac

我有一个用aiohttp构建的websocket服务器。 我一直在服务器错误流中获取此异常

Task exception was never retrieved
future: <Task finished coro=<read() done, defined at /usr/local/lib/python3.4/dist-packages/aiohttp/streams.py:576> exception=ClientDisconnectedError()>
Traceback (most recent call last):
  File "/usr/lib/python3.4/asyncio/tasks.py", line 234, in _step
    result = coro.throw(exc)
  File "/usr/local/lib/python3.4/dist-packages/aiohttp/streams.py", line 578, in read
    result = yield from super().read()
  File "/usr/local/lib/python3.4/dist-packages/aiohttp/streams.py", line 433, in read
    yield from self._waiter
  File "/usr/lib/python3.4/asyncio/futures.py", line 386, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.4/asyncio/tasks.py", line 287, in _wakeup
    value = future.result()
  File "/usr/lib/python3.4/asyncio/futures.py", line 275, in result
    raise self._exception
aiohttp.errors.ClientDisconnectedError
客户端代码为:

@asyncio.coroutine
def sync_store():
    resp = yield from aiohttp.get('http://localhost/stores/search')
    stores = yield from resp.json()
    total_page = stores['page']['total_page']
    page = stores['page']['current_page']
    total_resp = []
    ws_sockets = []
    while True:
        for page in range(page, total_page):
            session = aiohttp.ClientSession()
            ws = yield from asyncio.async(session.ws_connect('ws://localhost:8765/stores'))
            ws_sockets.append(ws)
            ws.send_str(json.dumps(stores['data']))
            resp = yield from asyncio.async(ws.receive())
            total_resp.append(resp.data)
            # print(resp)
            stores_resp = yield from asyncio.async(aiohttp.post('http://localhost/stores/search',
                                                                data=json.dumps({'page': page+1}),
                                                                headers={'content-type': 'application/json'}
                                                                ))
            stores = yield from asyncio.async(stores_resp.json())
        while ws_sockets:
            session = ws_sockets.pop(0)
            msg = yield from session.receive()
            if not(msg.tp == aiohttp.MsgType.close or msg.tp == aiohttp.MsgType.closed):
                ws_sockets.append(session)
            else:
                print(ws_sockets)
        break
    print(total_resp)
这有什么问题吗?
我还尝试启用调试模式,但似乎也没有提供任何有用的输出。

为什么要在
asyncio.async
中包装每个调用?如果您只是想立即从协同程序的结果中获得收益,则无需将其包装在未来的中,这只会增加不必要的开销。我添加了从的收益,以正确的顺序链接未来的结果,并将调用包装在asyncio中。async用于在等待时释放控件,因为这将是服务器的一部分,所以我不想阻止它,或者我误解了这里的某些内容?
@asyncio.coroutine
def sync(self, request):
    ws = web.WebSocketResponse()
    yield from ws.prepare(request)

    # while True:
    msg = yield from ws.receive()
    if msg.tp == aiohttp.MsgType.text:
        payload = msg.data
        pypayload = json.loads(payload)
        result = {'result': {}}
        for store in pypayload:
            try:
                sync_obj = yield from asyncio.async(self.prepare(store))
            except (IndexError, TypeError, ValidationError) as exc:
                yield from asyncio.async(self.handle_internal_error(exc, store))
            else:
                try:
                    sync_result, request_type = yield from asyncio.async(self.send_data(sync_obj))
                except DuplicateMappingsFound as exc:
                    yield from asyncio.async(self.handle_internal_error(exc, store))
                else:
                    if sync_result.status == 200 and request_type == 'post':
                        yield from asyncio.async(self.process_data(sync_result))
                    elif sync_result.status >= 400:
                        yield from asyncio.async(self.handle_error(sync_result, sync_obj))
                    result['result'].update(
                            {store['store_id']: sync_result.status}
                        )
                    yield from asyncio.async(sync_result.release())
        ws.send_str(json.dumps(result))
    elif msg.tp == aiohttp.MsgType.error:
        print('ws connection closed with exception {0}'.format(ws.exception()))

    yield from asyncio.async(ws.close())

    print('websocket connection closed')

    return ws
@asyncio.coroutine
def sync_store():
    resp = yield from aiohttp.get('http://localhost/stores/search')
    stores = yield from resp.json()
    total_page = stores['page']['total_page']
    page = stores['page']['current_page']
    total_resp = []
    ws_sockets = []
    while True:
        for page in range(page, total_page):
            session = aiohttp.ClientSession()
            ws = yield from asyncio.async(session.ws_connect('ws://localhost:8765/stores'))
            ws_sockets.append(ws)
            ws.send_str(json.dumps(stores['data']))
            resp = yield from asyncio.async(ws.receive())
            total_resp.append(resp.data)
            # print(resp)
            stores_resp = yield from asyncio.async(aiohttp.post('http://localhost/stores/search',
                                                                data=json.dumps({'page': page+1}),
                                                                headers={'content-type': 'application/json'}
                                                                ))
            stores = yield from asyncio.async(stores_resp.json())
        while ws_sockets:
            session = ws_sockets.pop(0)
            msg = yield from session.receive()
            if not(msg.tp == aiohttp.MsgType.close or msg.tp == aiohttp.MsgType.closed):
                ws_sockets.append(session)
            else:
                print(ws_sockets)
        break
    print(total_resp)