pypipython-3.x95% confidence\u2191 30

asyncpg - cannot perform operation: another operation is in progress

Full error message
I am attempting to resolve the following error:

asyncpg.exceptions._base.InterfaceError: cannot perform operation: another operation is in progress

Here is the full traceback:

Traceback (most recent call last):

  File "<string>", line 1, in <module>
  File "/Users/ddd/.pyenv/versions/3.8.6/lib/python3.8/multiprocessing/spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
               │     │   └ 4
               │     └ 7
               └ <function _main at 0x109c8aca0>
  File "/Users/ddd/.pyenv/versions/3.8.6/lib/python3.8/multiprocessing/spawn.py", line 129, in _main
    return self._bootstrap(parent_sentinel)
           │    │          └ 4
           │    └ <function BaseProcess._bootstrap at 0x109b1f8b0>
           └ <SpawnProcess name='SpawnProcess-4' parent=36604 started>
  File "/Users/ddd/.pyenv/versions/3.8.6/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap
    self.run()
    │    └ <function BaseProcess.run at 0x109b18ee0>
    └ <SpawnProcess name='SpawnProcess-4' parent=36604 started>
  File "/Users/ddd/.pyenv/versions/3.8.6/lib/python3.8/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
    │    │        │    │        │    └ {'config': <uvicorn.config.Config object at 0x109cd55b0>, 'target': <bound method Server.run of <uvicorn.server.Server object...
    │    │        │    │        └ <SpawnProcess name='SpawnProcess-4' parent=36604 started>
    │    │        │    └ ()
    │    │        └ <SpawnProcess name='SpawnProcess-4' parent=36604 started>
    │    └ <function subprocess_started at 0x10a4aca60>
    └ <SpawnProcess name='SpawnProcess-4' parent=36604 started>
  File "/Users/ddd/Desktop/repos/xxx/.venv/lib/python3.8/site-packages/uvicorn/subprocess.py", line 61, in subprocess_started
    target(sockets=sockets)
    │              └ [<socket.socket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('0.0.0.0', 80)>]
    └ <bound method Server.run of <uvicorn.server.Server object at 0x109cd56a0>>
  File "/Users/ddd/Desktop/repos/xxx/.venv/lib/python3.8/site-packages/uvicorn/server.py", line 48, in run
    loop.run_until_complete(self.serve(sockets=sockets))
    │    │                  │    │             └ [<socket.socket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('0.0.0.0', 80)>]
    │    │                  │    └ <function Server.serve at 0x10a4abca0>
    │    │                  └ <uvicorn.server.Server object at 0x109cd56a0>
    │    └ <function BaseEventLoop.run_until_complete at 0x10a205820>
    └ <_UnixSelectorEventLoop running=True closed=False debug=False>
  File "/Users/ddd/.pyenv/versions/3.8.6/lib/python3.8/asyncio/base_events.py", line 603, in run_until_complete
    self.run_forever()
    │    └ <function BaseEventLoop.run_forever at 0x10a205790>
    └ <_UnixSelectorEventLoop running=True closed=False debug=False>
  File "/Users/ddd/.pyenv/versions/3.8.6/lib/python3.8/asyncio/base_events.py", line 570, in run_forever
    self._run_once()
    │    └ <function BaseEventLoop._run_once at 0x10a209310>
    └ <_UnixSelectorEventLoop running=True closed=False debug=False>
  File "/Users/ddd/.pyenv/versions/3.8.6/lib/python3.8/asyncio/base_events.py", line 1859, in _run_once
    handle._run()
    │      └ <function Handle._run at 0x10a13ed30>
    └ <Handle <TaskWakeupMethWrapper object at 0x10bb75a60>(<_GatheringFu...in progress')>)>
  File "/Users/ddd/.pyenv/versions/3.8.6/lib/python3.8/asyncio/events.py", line 81, in _run
    self._context.run(self._callback, *self._args)
    │    │            │    │           │    └ <member '_args' of 'Handle' objects>
    │    │            │    │           └ <Handle <TaskWakeupMethWrapper object at 0x10bb75a60>(<_GatheringFu...in progress')>)>
    │    │            │    └ <member '_callback' of 'Handle' objects>
    │    │            └ <Handle <TaskWakeupMethWrapper object at 0x10bb75a60>(<_GatheringFu...in progress')>)>
    │    └ <member '_context' of 'Handle' objects>
    └ <Handle <TaskWakeupMethWrapper object at 0x10bb75a60>(<_GatheringFu...in progress')>)>

> File "./xxx/xxx/xxx.py", line 144, in get_disclosure_data
    hh_json, db_json = await asyncio.gather(*coroutines)
                             │       │       └ [<coroutine object xxxx at 0x10bb2cb40>, <coroutine object db_call at 0x10bb2cc40>]
                             │       └ <function gather at 0x10a1fad30>
                             └ <module 'asyncio' from '/Users/ddd/.pyenv/versions/3.8.6/lib/python3.8/asyncio/__init__.py'>

  File "./xxx/xxx/xxx.py", line 52, in db_call
    db_json = await asyncio.gather(*coroutines, loop=asyncio.get_event_loop())
                    │       │       │                │       └ <built-in function get_event_loop>
                    │       │       │                └ <module 'asyncio' from '/Users/ddd/.pyenv/versions/3.8.6/lib/python3.8/asyncio/__init__.py'>
                    │       │       └ [<coroutine object DBConnectionManager.fetch_item at 0x10bb434c0>, <coroutine object DBConnectionManager.fetch_item at 0x10bb...
                    │       └ <function gather at 0x10a1fad30>
                    └ <module 'asyncio' from '/Users/ddd/.pyenv/versions/3.8.6/lib/python3.8/asyncio/__init__.py'>

  File "./xxx/xxx/xx.py", line 97, in fetch_item
    await self._connection_pool.release(self.con)
          │    │                │       │    └ <PoolConnectionProxy [released] 0x10bbc9cd0>
          │    │                │       └ <chd_api.data.db.DBConnectionManager object at 0x10b946a30>
          │    │                └ <function Pool.release at 0x10b956a60>
          │    └ <asyncpg.pool.Pool object at 0x10bb131e0>
          └ <chd_api.data.db.DBConnectionManager object at 0x10b946a30>

  File "/Users/ddd/Desktop/repos/xxx/.venv/lib/python3.8/site-packages/asyncpg/pool.py", line 666, in release
    return await asyncio.shield(ch.release(timeout))
                 │       │   

The assignment to self.con in fetch_item causes multiple coroutines to share the same connection. While you do want them to share the connection pool, sharing the same connection doesn't make sense, as the connection is stateful. To resolve the issue, replace usage of self.con with a local variable con.

API access

Get this solution programmatically \u2014 free, no authentication.

curl https://depscope.dev/api/error/33ce6896d923b2173f0618a9c998396cf1b066a94f8c0eafb6fe76172f3f347e
hash \u00b7 33ce6896d923b2173f0618a9c998396cf1b066a94f8c0eafb6fe76172f3f347e
asyncpg - cannot perform operation: another operation is in… — DepScope fix | DepScope