Tornado Signal Processing

Source: Internet
Author: User
Tags sigint signal

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.