π₯οΈ Web Server Gateway (WSGI)ΒΆ
Duckβs WSGI implementation brings you the best of both worlds:
β‘ Traditional multithreading for sync workloads, plus support for async protocols like HTTP/2 & WebSockets.
Unlike most WSGI servers, Duckβs version isnβt limited β it runs a background event loop so async tasks can still flow smoothly π.
π§ Enabling WSGI ModeΒΆ
In your settings.py, simply set:
ASYNC_HANDLING = False
This switches the server into WSGI (synchronous) mode.
π Working with the Event LoopΒΆ
Duck automatically runs a background asyncio event loop to handle async protocols even in sync mode.
You can access it with:
duck.utils.asyncio.eventloop.get_or_create_loop_manager
π Run coroutines in the background with
submit_task.π Returns either an async future or a sync future (so you can
.wait()in synchronous code).π Use
help(duck.utils.asyncio.eventloop)for more details.
π NotesΒΆ
β The default WSGI does everything for you β only override if necessary.
π In synchronous environments, define your views as regular sync functions.
β‘ Async views are also supported β theyβll be automatically converted to sync.
π¨βπ» Defining Sync ViewsΒΆ
1οΈβ£ Function-based ViewsΒΆ
# views.py
def myview(request):
# Some code here to return HttpResponse
...
2οΈβ£ Class-based ViewsΒΆ
# views.py
from duck.views import View
class MyView(View):
def run(self):
# Some code to return HttpResponse
...
β¨ With Duckβs WSGI, you get classic sync stability with a touch of modern async compatibility.
Perfect for apps that need threading performance while still being ready for protocols like WebSockets & HTTP/2 π
π Looking for full async mode? Check out β‘ ASGI