Search code examples
djangoechoreal-time

Django: Real-time logging to browser


In PHP, I used to be able to use the echo command and see scripts in real time.

For example:

Applying settings to 001...
* a second later *
Applying settings to 002...

This would be shown to the user in the browser. Is there anyways I can use a command like echo in Django? It's nice to know my application is actually doing something and seeing the progress.


Solution

  • It's nice to know my application is actually doing something and seeing the progress.

    I'm assuming you want to inform an actual user of the progress of some long running task, and not merely print debugging statements. As for debugging or code tracing, use Django's logging subsystem in combination with django-debugtoolbar.

    Is there anyways I can use a command like echo in Django?

    Since Django 1.5, there is a StreamingHttpResponse that kind of allows you to emulate php's echo behavior, under some circumstances.

    However, take into account what it says in the Documentation:

    Django is designed for short-lived requests. Streaming responses will tie a worker process for the entire duration of the response. This may result in poor performance.

    That said, there is an example.

    Really?

    Note that the recommended and stable way of doing this, in any HTTP request/response driven context (which Django is designed for), is to use an asynchronous task processor like Celery for any long-lived processes. Then use a polling client to check on the progress, e.g. as proposed in this answer.