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

发布于 2022-09-11 18:59:19 字数 3605 浏览 42 评论 0

本人想在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中,不知道从哪方面入手,请各位指点

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹沉浮 2022-09-18 18:59:19

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文