question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

something wrong in my app with error "close cannot be used while an asynchronous query is underway"

See original GitHub issue

Renctly I tried to redeploy my project used with sanic frame ,some errors happened when I do a test under high-concurrency. info in the log:

close cannot be used while an asynchronous query is underway

and the traceback is below:

Traceback (most recent call last):
  File "/home/mwh/ad_sanic/handlers/oprs.py", line 17, in post
    _parsed_req = await ReqParser(_req_body,'parse',req_ip=(request.headers['Remote-Addr']).split(':')[0]).parser()
  File "/home/mwh/ad_sanic/utils/req_parse.py", line 49, in parser
    _mapped['sim_country_iso'] = await self.geo()
  File "/home/mwh/ad_sanic/utils/req_parse.py", line 75, in geo
    db_country = (await self.db.get(DbIp.select(DbIp.country).where((DbIp.ip_from<=self._req_ip)&(DbIp.ip_to>=self._req_ip)))).country
  File "/home/mwh/py3.5_virtuanlenv/lib/python3.5/site-packages/peewee_async.py", line 169, in get
    result = yield from self.execute(query.limit(1))
  File "/home/mwh/py3.5_virtuanlenv/lib/python3.5/site-packages/peewee_async.py", line 269, in execute
    return (yield from execute(query))
  File "/home/mwh/py3.5_virtuanlenv/lib/python3.5/site-packages/peewee_async.py", line 428, in execute
    return (yield from coroutine(query))
  File "/home/mwh/py3.5_virtuanlenv/lib/python3.5/site-packages/peewee_async.py", line 569, in select
    cursor = yield from _execute_query_async(query)
  File "/home/mwh/py3.5_virtuanlenv/lib/python3.5/site-packages/peewee_async.py", line 1491, in _execute_query_async
    return (yield from _run_sql(query.database, *query.sql()))
  File "/home/mwh/py3.5_virtuanlenv/lib/python3.5/site-packages/peewee_async.py", line 1476, in _run_sql
    cursor = yield from database.cursor_async()
  File "/home/mwh/py3.5_virtuanlenv/lib/python3.5/site-packages/peewee_async.py", line 902, in cursor_async
    return (yield from self._async_conn.cursor(conn=conn))
  File "/home/mwh/py3.5_virtuanlenv/lib/python3.5/site-packages/peewee_async.py", line 1067, in cursor
    conn = yield from self.acquire()
  File "/home/mwh/py3.5_virtuanlenv/lib/python3.5/site-packages/peewee_async.py", line 1036, in acquire
    return (yield from self.pool.acquire())
  File "/home/mwh/py3.5_virtuanlenv/lib/python3.5/site-packages/aiopg/utils.py", line 67, in __iter__
    resp = yield from self._coro
  File "/home/mwh/py3.5_virtuanlenv/lib/python3.5/site-packages/aiopg/pool.py", line 180, in _acquire
    yield from self._cond.wait()
  File "/usr/local/lib/python3.5/asyncio/locks.py", line 326, in wait
    yield from fut
  File "/usr/local/lib/python3.5/asyncio/futures.py", line 385, in __iter__
    yield self  # This tells Task to wait for completion.
  File "uvloop/future.pyx", line 434, in uvloop.loop.BaseTask._fast_wakeup (uvloop/loop.c:112710)
  File "/usr/local/lib/python3.5/asyncio/futures.py", line 266, in result
    raise CancelledError
concurrent.futures._base.CancelledError

what caused this problem?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:8
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
hexazerocommented, Mar 29, 2018

I was getting this same issue.

peewee.ProgrammingError: close cannot be used while an asynchronous query is underway

After tracing through the issue, I determined it was due to my setup with pytest-asyncio since that library does the following in the default event_loop fixture:

Creates and injects a new instance of the default asyncio event loop. By default, the loop will be closed at the end of the test (i.e. the default fixture scope is function).

To fix the issue, I created a conftest.py file at the root of my tests that has the following replacement fixture.

import pytest
import asyncio


@pytest.fixture(scope='session')
def event_loop():
    return asyncio.new_event_loop()

Maybe this will help someone else who lands here from google.

0reactions
ahopkinscommented, Mar 5, 2018

I get this while performing unit tests.

I have a series of simple tests to grab an entity with a simple manager context query. If I try to execute more than one during a test, I get this error.

_common/common/db/getters.py:82: in get_generic
    return await entity_getter(node_tag=node_tag, client_code=client_code)
_common/common/db/getters.py:17: in get_store
    Client.code ** client_code
/home/adam/.virtualenvs/mxtest/lib/python3.6/site-packages/peewee_async.py:169: in get
    result = yield from self.execute(query)
/home/adam/.virtualenvs/mxtest/lib/python3.6/site-packages/peewee_async.py:269: in execute
    return (yield from execute(query))
/home/adam/.virtualenvs/mxtest/lib/python3.6/site-packages/peewee_async.py:433: in execute
    return (yield from coroutine(query))
/home/adam/.virtualenvs/mxtest/lib/python3.6/site-packages/peewee_async.py:574: in select
    cursor = yield from _execute_query_async(query)
/home/adam/.virtualenvs/mxtest/lib/python3.6/site-packages/peewee_async.py:1555: in _execute_query_async
    return (yield from _run_sql(query.database, *query.sql()))
/home/adam/.virtualenvs/mxtest/lib/python3.6/site-packages/peewee_async.py:1542: in _run_sql
    return cursor
/home/adam/.virtualenvs/mxtest/lib/python3.6/site-packages/peewee.py:3656: in __exit__
    reraise(new_type, new_type(*exc_args), traceback)
/home/adam/.virtualenvs/mxtest/lib/python3.6/site-packages/peewee.py:135: in reraise
    raise value.with_traceback(tb)
/home/adam/.virtualenvs/mxtest/lib/python3.6/site-packages/peewee_async.py:1539: in _run_sql
    yield from cursor.release
/usr/lib/python3.6/asyncio/coroutines.py:210: in coro
    res = func(*args, **kw)
/home/adam/.virtualenvs/mxtest/lib/python3.6/site-packages/peewee_async.py:1107: in release_cursor
    cursor.close()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <aiopg.cursor.Cursor object at 0x7fc48e910fd0>

    def close(self):
        """Close the cursor now."""
>       self._impl.close()
E       peewee.ProgrammingError: close cannot be used while an asynchronous query is underway

/home/adam/.virtualenvs/mxtest/lib/python3.6/site-packages/aiopg/cursor.py:51: ProgrammingError
Read more comments on GitHub >

github_iconTop Results From Across the Web

ERROR: Execute cannot be used while an asynchronous ...
It happens a lot when auto-suggest is on. To switch off autocomplete go to File > Preferences > Query Tool > Auto Completion...
Read more >
execute cannot be used while an asynchronous query is ...
ERROR : execute cannot be used while an asynchronous query is underway. reproduce step: 1. open a query tool for any database in...
Read more >
close cannot be used while an asynchronous query is underway
the problem is that self._weak = weakref.ref(self, lambda wr: cursor.close()) try to close the connection when a query is underway (as you read ......
Read more >
DatabaseError: execute used with asynchronous query
DatabaseError : execute cannot be used while an asynchronous query is underway. I can't seem to reproduce the error, it's never happened before,...
Read more >
Asynchronous Query Execution - Devart
Later, when the query has finished its execution, application calls the "End" method, passing it IAsyncResult object returned by the "Begin" method that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found