django-debug-toolbar icon indicating copy to clipboard operation
django-debug-toolbar copied to clipboard

gather information from async functions

Open smmoosavi opened this issue 4 years ago • 7 comments

If some queries are executed in async mode, these queries are missing in the panel. How can we collect these?

data = asyncio.run(parallel_calc_aggregations(queryset, aggregations))

note1: I am using django-debug-toolbar via django-graphiql-debug-toolbar note2: all queries are inside function wrapped by sync_to_async

The same issue exists when graphql ThreadExecutor is used. read more about graphql executors graphql-314

smmoosavi avatar Dec 28 '20 17:12 smmoosavi

The toolbar currently does not support async and the collection of statistics is tightly coupled to a request. If you do end up diving into how to modify the toolbar to collect this information, we'd appreciate any conclusions or findings of yours.

tim-schilling avatar Dec 28 '20 17:12 tim-schilling

Is there any decorator function we can add around these functions and collect data required by debug-toolbar?

@collect_debug_toolbar_data
def get_data():
  # pure django

async def async_get_data():
      return await sync_to_async(get_data, thread_sensitive=False)()

async def parallel_get_data():
  return await asyncio.gather(async_get_data(), async_get_data(), async_get_data())

data = asyncio.run(parallel_get_data())

smmoosavi avatar Dec 28 '20 18:12 smmoosavi

Currently the only API for collecting the data is done via the middleware and the methods on the panels themselves.

tim-schilling avatar Dec 28 '20 18:12 tim-schilling

maybe it's possible to wrap/runtime patch sync_to_async. all async function using django orm should use this function and without sync_to_async function causing error.

def sync_to_async(func=None, thread_sensitive=True):
    if func is None:
        return lambda f: SyncToAsync(f, thread_sensitive=thread_sensitive)
    return SyncToAsync(func, thread_sensitive=thread_sensitive)

the func argument is a simple sync function

smmoosavi avatar Dec 28 '20 18:12 smmoosavi

Until async views are supported can the toolbar at least be kept from erroring out on aync views?

michaelurban avatar Aug 29 '23 18:08 michaelurban

@michaelurban that's a good idea. Or at least log some type of message. Would you be able to write up a PR to introduce that feature?

tim-schilling avatar Aug 29 '23 18:08 tim-schilling

@tim-schilling Sorry, I don't have the time. I'm working 80+ hour weeks for the foreseeable future.

michaelurban avatar Aug 29 '23 19:08 michaelurban