Django: How to run a function when server exits?

前端 未结 2 1876
独厮守ぢ
独厮守ぢ 2021-01-12 09:24

I am writing a Django project where several processes are opened using Popen. Right now, when the server exits, these processes are orphaned. I have a function to terminate

2条回答
  •  抹茶落季
    2021-01-12 09:43

    First of all "When the server quits" is ambiguous. Does this stuff run when responding to a request? Does this stuff run during a management command?

    Let's assume for the sake of argument, that you are running this somewhere in a view, so you want to have something that runs after each view returns in order to clean up junk that the view left hanging around.

    Most likely, what you are looking to do is to write some Middleware. Even more specifically, some sort of process_response.

    However, based on the short description of what you have so far, it sounds far more likely that you should be using some task manager, such as Celery to manage asynchronous tasks and processes.

提交回复
热议问题