dongweiming / lyanna

My Blog Using Sanic
http://www.dongwm.com
GNU General Public License v3.0
637 stars 173 forks source link

InterfaceError: cannot perform operation: another operation is in progress #48

Closed waketzheng closed 4 years ago

waketzheng commented 4 years ago

ipython中执行await Model.all()报错了:

Python 3.8.2 (default, Apr  2 2020, 07:37:04)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from ext import *;from models import *;await init_db();

In [2]: await PrepayRecord.all()

完整错误信息如下:

---------------------------------------------------------------------------
InterfaceError                            Traceback (most recent call last)
<ipython-input-2-efe4a7ae9889> in <module>
----> 1 await PrepayRecord.all()

~/.local/share/virtualenvs/wanghai-WxqqJLPL/lib/python3.8/site-packages/tortoise/queryset.py in _execute(self)
    641
    642     async def _execute(self) -> List[MODEL]:
--> 643         instance_list = await self._db.executor_class(
    644             model=self.model,
    645             db=self._db,

~/.local/share/virtualenvs/wanghai-WxqqJLPL/lib/python3.8/site-packages/tortoise/backends/base/executor.py in execute_select(self, query, custom_fields)
    103
    104     async def execute_select(self, query: Query, custom_fields: Optional[list] = None) -> list:
--> 105         _, raw_results = await self.db.execute_query(query.get_sql())
    106         instance_list = []
    107         for row in raw_results:

~/.local/share/virtualenvs/wanghai-WxqqJLPL/lib/python3.8/site-packages/tortoise/backends/asyncpg/client.py in translate_exceptions_(self, *args)
     34     async def translate_exceptions_(self, *args):
     35         try:
---> 36             return await func(self, *args)
     37         except asyncpg.SyntaxOrAccessError as exc:
     38             raise OperationalError(exc)

~/.local/share/virtualenvs/wanghai-WxqqJLPL/lib/python3.8/site-packages/tortoise/backends/asyncpg/client.py in execute_query(self, query, values)
    171             else:
    172                 rows = await connection.fetch(*params)
--> 173                 return len(rows), rows
    174
    175     @translate_exceptions

~/.local/share/virtualenvs/wanghai-WxqqJLPL/lib/python3.8/site-packages/tortoise/backends/base/client.py in __aexit__(self, exc_type, exc_val, exc_tb)
    303     async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
    304         # release the connection back to the pool
--> 305         await self.pool.release(self.connection)
    306
    307

~/.local/share/virtualenvs/wanghai-WxqqJLPL/lib/python3.8/site-packages/asyncpg/pool.py in release(self, connection, timeout)
    652         # does not prevent the connection from being returned to the
    653         # pool properly.
--> 654         return await asyncio.shield(ch.release(timeout))
    655
    656     async def close(self):

~/.local/share/virtualenvs/wanghai-WxqqJLPL/lib/python3.8/site-packages/asyncpg/pool.py in release(self, timeout)
    214                 self._con.terminate()
    215             finally:
--> 216                 raise ex
    217
    218         # Free this connection holder and invalidate the

~/.local/share/virtualenvs/wanghai-WxqqJLPL/lib/python3.8/site-packages/asyncpg/pool.py in release(self, timeout)
    204                     budget -= time.monotonic() - started
    205
--> 206             await self._con.reset(timeout=budget)
    207         except (Exception, asyncio.CancelledError) as ex:
    208             # If the `reset` call failed, terminate the connection.

~/.local/share/virtualenvs/wanghai-WxqqJLPL/lib/python3.8/site-packages/asyncpg/connection.py in reset(self, timeout)
   1112
   1113         if reset_query:
-> 1114             await self.execute(reset_query, timeout=timeout)
   1115
   1116     def _abort(self):

~/.local/share/virtualenvs/wanghai-WxqqJLPL/lib/python3.8/site-packages/asyncpg/connection.py in execute(self, query, timeout, *args)
    270
    271         if not args:
--> 272             return await self._protocol.query(query, timeout)
    273
    274         _, status, _ = await self._execute(query, args, 0, timeout, True)

~/.local/share/virtualenvs/wanghai-WxqqJLPL/lib/python3.8/site-packages/asyncpg/protocol/protocol.pyx in query()

~/.local/share/virtualenvs/wanghai-WxqqJLPL/lib/python3.8/site-packages/asyncpg/protocol/protocol.pyx in asyncpg.protocol.protocol.BaseProtocol._check_state()

InterfaceError: cannot perform operation: another operation is in progress

这个是我代码的问题,还是asyncpg的BUG呀?

waketzheng commented 4 years ago

看了tortoise-orm的源码 发现只需执行await Tortoise.close_connections() 然后再重新await init_db() 问题就解决了uuu