Skip to main content

Tools for replacing `sync_to_async` calls to `sync_to_async(thread_sensitive=False, executor=...)`, effectively allowing Django to make calls to database concurrently

Project description

django_threaded_sync_to_async

Tools for replacing sync_to_async calls to sync_to_async(thread_sensitive=False, executor=...), effectively allowing Django to make calls to database concurrently.

pip3 install django_threaded_sync_to_async

Executor

import django_threaded_sync_to_async

async with django_threaded_sync_to_async.Executor(thread_name_prefix="thread", max_workers=3) as executor:
    a = asgiref.sync.sync_to_async(long_call)(1)
    b = asgiref.sync.sync_to_async(long_call)(2)
    c = asgiref.sync.sync_to_async(long_call)(3)
    d = asgiref.sync.sync_to_async(long_call)(4)
    await asyncio.gather(a, b, c, d)

SharedExecutor

Maintains global dictionary of executors (concurrent.futures.ThreadPoolExecutor) accessed by name and allows to limit utilization of executor for a single context.

import django_threaded_sync_to_async

@django_threaded_sync_to_async.SharedExecutor("common", max_workers=3, max_tasks=2)
async def operations():
    a = asgiref.sync.sync_to_async(long_call)(1)
    b = asgiref.sync.sync_to_async(long_call)(2)
    c = asgiref.sync.sync_to_async(long_call)(3)
    d = asgiref.sync.sync_to_async(long_call)(4)
    await asyncio.gather(a, b, c, d)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_threaded_sync_to_async-1.0.4.tar.gz (6.7 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page