How to Redirect Stdout to Streaming Response in Django

Sometimes we need to execute some long tasks at the backend, and the tasks are complicated and error-prone. So we hope users can see the real-time console log. Thus we need to redirect the stdout in our functions to the user’s browser. Given a function like the following. How to see the stdout in real-time in the browser? import time def job(times): for i in range(times): print(f'Task #{i}') time.sleep(1) print('Done') time.sleep(0.5) Streaming Response Normally, responses are sent after all the data has been collected. However, sometimes we can’t wait until the data is prepared. In this case, we’ll use streaming...

May 25, 2021 · 2 min · wancat