How To Break Django: With Async
summary
Django 3.1 accepts async def views but database and many libraries remain synchronous so calling ORM code from async raises a SynchronousOnlyOperation error and blocks the event loop if misused. Use sync_to_async to run DB work in a worker thread but beware transactions and connection objects are threadbound so prior code can silently corrupt data. Pythonβs asyncio debug flags long-running coroutines and forgotten awaits. Recommended workflow is sync first, profile, then convert hot paths to async.
AI-written summary Β· about
chapters (16)
- 0:00Intro
- 2:17Synchronous programming is quite safe
- 2:47Async programming is hard But it can definitely be worth it!
- 6:44SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async
- 8:33Django's got your back ..this time, anyway.
- 9:47There's no ordering guarantee! Or even a guarantee it will execute.
- 10:46await create_user_account() await send_created_email()
- 11:48Race conditions! Names are power - now you can research it!
- 13:22await verify_valid_user(...) await create_user_account(...)
- 15:39You don't actually have a transaction. We need to address this when we make the ORM async-aware!
- 18:54Bad with threads. Good with async! The await model brings us atomic sections for free
- 24:42How do we defend against these? Seems awfully easy to slip up, doesn't it.
- 27:19SynchronousOnlyOperation Django's here to save you from the worst errors, where we can
- 27:56Again, async programming is hard This is not just Python!
- 29:05you can quickly & safely add async.
- 29:49We'll get better patterns! Until then, be safe out there
