Skip to content
mispy Notes from a night desk

Long polling in Django with gevent and AMQP

Django is comfortable with short requests. Holding one open while a worker computes an answer requires a different set of habits.

A normal web request has a simple contract: the client asks, the server answers, the connection closes. Long polling breaks the middle step. The client asks, the server waits, and the connection stays open until there is something to say. It is a useful trick for updates that arrive on their own schedule, but it changes the economics of the server.

Django does not ship with an asynchronous request model. That does not make long polling impossible. It makes the choice of worker important. A server process that blocks on a sleeping request can only handle a handful of waiting clients before it runs out of room.

Let the greenlets wait

gevent gives Python a cooperative form of concurrency. Thousands of lightweight tasks can be alive at once, and when one of them waits on a network operation, another can run. The key word is cooperative. Code has to yield, either by using patched libraries or by waiting on operations that know how to yield. A tight CPU loop will still block everyone.

Running Django under a gevent worker is mostly a deployment decision. The application code can stay synchronous, which is a relief for a codebase that was not written with async in mind. The cost is that every blocking call in the path needs to be compatible with the concurrency model. A stray library that uses a raw socket can quietly serialize the whole party.

Give each request a reply address

AMQP is a good fit for the waiting part. A request is published to a queue. The message carries a reply address, which is a temporary queue created for this one caller. A worker consumes the request, does the work, and publishes the result to that reply address. The waiting process reads from its private queue and returns the answer to the browser.

RequestA JSON body and a reply queue name.
WorkerConsumes the request, computes, publishes the result.
WaitA read with a timeout, so a lost message does not hold the socket forever.

Timeouts are part of the design

Every waiting request needs a deadline. If the worker crashes, the reply never arrives, and the browser sits on a connection that looks alive but is going nowhere. A timeout turns that silence into an error the client can handle. The error should be honest: the work may still finish later, but this particular request gave up.

Long polling is a loan against the future. A timeout is the interest rate. Ignore it and the debt arrives all at once.

When it is worth it

This pattern works well when updates are infrequent and the work is expensive enough to justify a queue. It is a poor fit for a chat application with thousands of tiny messages, where a streaming protocol or a dedicated real time service will be simpler. The architecture should follow the shape of the traffic, not the fashion of the month.

Used carefully, the combination is pleasant: Django stays familiar, gevent keeps the waiting cheap, and AMQP gives the work a place to happen that is not inside the web process.