Django

Django Async & ASGI

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

26 면접 질문·
Senior
1

What is the main difference between WSGI and ASGI?

답변

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?

답변

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?

답변

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 면접 질문

다음 면접을 위해 Django을 마스터하세요

모든 질문, flashcards, 기술 테스트, 코드 리뷰 연습, 면접 시뮬레이터에 접근하세요.

무료로 시작하기