πŸ–₯️ Web Server Gateway (WSGI)ΒΆ

Sync Ready
Threaded
Async Compatible
Duck Framework


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