Django

Django Async & ASGI

ASGI vs WSGI, async views, sync_to_async, async_to_sync, sync ORM limitations, async performance, database connection pooling

26 perguntas de entrevista·
Senior
1

What is the main difference between WSGI and ASGI?

Resposta

ASGI (Asynchronous Server Gateway Interface) extends WSGI by adding support for asynchronous programming and persistent connections like WebSockets. WSGI is synchronous and can only handle one request at a time per worker, while ASGI can handle multiple requests simultaneously in a single process through the asyncio event loop.

2

How to declare an async view in Django?

Resposta

An async view in Django is declared simply by using the async keyword before def. Django automatically detects whether the view is asynchronous and runs it in the appropriate event loop. This native Python 3.5+ syntax has been fully supported since Django 3.1.

3

What is the role of sync_to_async in Django?

Resposta

sync_to_async is an adapter provided by asgiref that allows running synchronous code within an asynchronous context. It wraps a synchronous function and executes it in a separate thread pool, preventing blocking of the event loop. This is essential for calling the Django ORM from an async view since the ORM is synchronous.

4

Why can't the Django ORM be used directly in an async view?

5

What is the role of async_to_sync in Django?

+23 perguntas de entrevista

Domine Django para sua proxima entrevista

Acesse todas as perguntas, flashcards, testes tecnicos, exercicios de code review e simuladores de entrevista.

Comece gratis