tornado多进程模式使用aioredis出现InvalidStateError

本人想在tornado项目中使用aioredis,为了更好的性能tornado使用了自带的多进程即server.start(0)。但是在启动和压力测试阶段会有概率出现asyncio.base_futures.InvalidStateError: invalid state错误,并且在启动的时候有概率报OSError: [Errno 107] Transport endpoint is not connected,不知道是什么原因导致的以及如何解决,代码如下

from tornado import ioloop, web, httpserver
import asyncio, aioredis

class REDIS(object):
    @staticmethod
    async def initialize(loop):
        config = {
        }
    
        host = config.get('host', '127.0.0.1')
        port = config.get('port', 6379)
        minsize = config.get('minsize', 1)
        maxsize = config.get('maxsize', 5)
        db = config.get('db', 0)
        
        REDIS.pool = await aioredis.create_pool(
                'redis://{host}:{port}/{db}'.format(host=host, port=port, db=db),
                minsize=minsize,
                maxsize=maxsize,
                loop=loop)

class MainHandler(web.RequestHandler):
    def post(self):
        self.write('abc')

def make_app():
    return web.Application([
        (r"/", MainHandler),
    ])

if __name__ == '__main__':
    app = make_app()
    
    server = httpserver.HTTPServer(app)
    server.listen(9999)
    server.start(0)
    
    loop = asyncio.get_event_loop()
    loop.run_until_complete(REDIS.initialize(loop))
    
    print('server is running')
    ioloop.IOLoop.current().start()

报错如下

Traceback (most recent call last):
  File "test.py", line 41, in <module>
    loop.run_until_complete(REDIS.initialize(loop))
  File "/usr/lib64/python3.6/asyncio/base_events.py", line 468, in run_until_complete
    return future.result()
  File "test.py", line 22, in initialize
    loop=loop)
server is running
  File "/root/amoreFuli/lib/python3.6/site-packages/aioredis/pool.py", line 56, in create_pool
    await pool._fill_free(override_min=False)
  File "/root/amoreFuli/lib/python3.6/site-packages/aioredis/pool.py", line 388, in _fill_free
    conn = await self._create_new_connection(self._address)
  File "/root/amoreFuli/lib/python3.6/site-packages/aioredis/connection.py", line 112, in create_connection
    address = sock.getpeername()
OSError: [Errno 107] Transport endpoint is not connected
Exception in callback BaseSelectorEventLoop._sock_connect_cb(<Future finished result=None>, <socket.socke...233.4', 6379)>, ('192.168.233.4', 6379))
handle: <Handle BaseSelectorEventLoop._sock_connect_cb(<Future finished result=None>, <socket.socke...233.4', 6379)>, ('192.168.233.4', 6379))>
Traceback (most recent call last):
  File "/usr/lib64/python3.6/asyncio/events.py", line 145, in _run
    self._callback(*self._args)
  File "/usr/lib64/python3.6/asyncio/selector_events.py", line 488, in _sock_connect_cb
    fut.set_result(None)
asyncio.base_futures.InvalidStateError: invalid state
Exception in callback BaseSelectorEventLoop._sock_connect_cb(<Future finished result=None>, <socket.socke...233.4', 6379)>, ('192.168.233.4', 6379))
handle: <Handle BaseSelectorEventLoop._sock_connect_cb(<Future finished result=None>, <socket.socke...233.4', 6379)>, ('192.168.233.4', 6379))>
Traceback (most recent call last):
  File "/usr/lib64/python3.6/asyncio/events.py", line 145, in _run
    self._callback(*self._args)
  File "/usr/lib64/python3.6/asyncio/selector_events.py", line 488, in _sock_connect_cb
    fut.set_result(None)
asyncio.base_futures.InvalidStateError: invalid state
server is running
server is running
child 0 (pid 12795) exited with status 1, restarting
server is running

因为报错主要集中在Python自带的asyncio中,不知道从哪方面入手,请各位指点

阅读 1.4k
评论
    1 个回答
    青木长青
    • 0
    • 新人请关照

    嗨, 这个问题解决了么,我也遇到同样的问题了。

    该答案已被忽略,原因:

    评论 赞赏
      撰写回答

      登录后参与交流、获取后续更新提醒