# \_AssertNumQueriesContext not async compatible

**URL:** <https://forum.djangoproject.com/t/assertnumqueriescontext-not-async-compatible/35360>\
**Category:** Async\
**Created:** [October 3, 2024, 3:29pm UTC](https://forum.djangoproject.com/t/assertnumqueriescontext-not-async-compatible/35360 "2024-10-03T15:29:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AMontagu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/amontagu/32/24330_2.png) [@AMontagu](https://forum.djangoproject.com/u/AMontagu)\
**Post date:** [October 3, 2024, 3:29pm UTC](https://forum.djangoproject.com/t/assertnumqueriescontext-not-async-compatible/35360/1 "2024-10-03T15:29:41Z")

</div>

Hello

I just tryed to use self.assertNumQueries in full async test.

Code ref: [django/django/test/testcases.py at 1feedc8ef8a34484cb5afe33f5c45b543b860210 · django/django · GitHub](https://github.com/django/django/blob/1feedc8ef8a34484cb5afe33f5c45b543b860210/django/test/testcases.py#L1288)

I also tryed to hack the issue like:

```auto
async with self.assertNumQueries(1):
  await self.async_method(some_kwargs=some_kwargs)

```

or

```auto
await sync_to_async(self.assertNumQueries)(1, func=async_method, some_kwargs=some_kwargs)
...
def assertNumQueries(self, num, func=None, *args, using=DEFAULT_DB_ALIAS, **kwargs):
        conn = connections[using]

        context = _AssertNumQueriesContext(self, num, conn)
        if func is None:
            return context

        with context:
            async_to_sync(func)(*args, **kwargs)

```

But obviously the event loop is not find event if a new one is created.

It doesn’t look too hard to add **aenter** and **aexit** methods to \_AssertNumQueriesContext that then call the original method but in a sync context. I may be ablea to do it by myself. Just wanted to check if there is maybe another obvious solution I missed

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [October 3, 2024, 5:35pm UTC](https://forum.djangoproject.com/t/assertnumqueriescontext-not-async-compatible/35360/2 "2024-10-03T17:35:10Z")

</div>

No, it’s not currently compatible.

See here:

[https://code.djangoproject.com/ticket/35388](https://code.djangoproject.com/ticket/35388)

Pending a proof-of-concept from someone. (I recall @felixxm putting quite a lot of thought into it previously without success)

---

<div class="post-metadata">

**Author:** ![AMontagu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/amontagu/32/24330_2.png) [@AMontagu](https://forum.djangoproject.com/u/AMontagu)\
**Post date:** [October 7, 2024, 3:23pm UTC](https://forum.djangoproject.com/t/assertnumqueriescontext-not-async-compatible/35360/3 "2024-10-07T15:23:59Z")

</div>

Thank you for your answer. I see it look more easy that it really is. Will not use it for now.
