In General, for online programs, we can't take kill-9 to kill the process.
Because the program may have an unhandled program, if you take kill-9, it may result in inconsistent data
What if we need to close the program, in general we take the signal technology
When we press CTRL + C, the program receives the SIGINT signal.
The program receives a sigterm signal when the kill PID command is sent
After the program receives these signals, we need to do a processing, such as shutting down the listening port no longer receive new requests
Then wait 60 seconds (the wait time depends on your needs) for outstanding processing.
Close the program after waiting for the end
We take tornado as an example, the sample code is as follows:
123456789101112131415161718192021222324252627282930 |
Def Sig_handler (SIG, Frame): logging.warning (' Caught signal:%s ', SIG) Tornado.ioloop.IOLoop.instance (). Add_callback (shutdown) def shutdown (): logging.info (' stopping HTTP server ') http_server.stop () Logging.info (' would Shutdown in%s seconds ... ', $) Io_loop = Tornado.ioloop.IOLoop.instance () deadline = Time.time () + Def stop_loop (): now = Time.time () If now < deadline and (Io_loop._callbacks or io_loop._timeouts): Io_loop.add_timeout (now + 1, stop_loop) else:io_loop.stop () logging.info (' Shutdown ') Stop_loop () if __name__ = = "__main__": tornado.options.parse_command_line () Http_server = Tornado.httpser Ver. Httpserver (Application ()) Http_server.listen (options.port) logging.info (' Start listen on port:%s ', options.port) Signal.signal (signal. SIGTERM, Sig_handler) signal.signal (signal. SIGINT, Sig_handler) tornado.ioloop.IOLoop.instance (). Start () |
Tornado Signal Processing