Django: Subprocess Continuous Output To HTML View

前端 未结 2 871
甜味超标
甜味超标 2021-02-08 23:23

I need a HTML webpage in my Django app to load and show the continous output of a script in a scrollable box. Is this possible?

I\'m presently using a subprocess

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-08 23:54

    You could simplify your task using StreamingHttpResponse and Popen:

    def test_iterator():
        from subprocess import Popen, PIPE, CalledProcessError
    
        with Popen(['ping', 'localhost'], stdout=PIPE, bufsize=1, universal_newlines=True) as p:
            for line in p.stdout:
                yield(line + '
    ') # process line here if p.returncode != 0: raise CalledProcessError(p.returncode, p.args) def busy_view(request): from django.http import StreamingHttpResponse return StreamingHttpResponse(test_iterator())

    StreamingHttpResponse expects an iterator as its parameter. An iterator function is one which has a yield expression (or a generator expression), and its return value is a generator object (an iterator).

    In this example, I simply echo the ping command to prove it works.

    Substitute ['ping', 'localhost'] by a list (it has to be a list if you pass parameters to the command - in this case, localhost). Your original ['python', script] should work.

    If you want to know more about generators, I would recommend Trey Hunner's talk, and also strongly that you read chapter 14 of Fluent Python book. Both are amazing sources.

    Disclaimer:

    Performance considerations

    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.

    Generally speaking, you should perform expensive tasks outside of the request-response cycle, rather than resorting to a streamed response.

提交回复
热议问题