Python daemon usage instance analysis, python daemon

Source: Internet
Author: User

Python daemon usage instance analysis, python daemon

This example describes how to use the Python daemon. Share it with you for your reference. The specific analysis is as follows:

The daemon can always run without blocking the exit of the main program. To mark a daemon Process, you can set the daemon attribute of the Process instance to True. The Code is as follows:

Import osimport timeimport randomimport sysfrom multiprocessing import Process, current_processdef daemon (): p = current_process () print "starting ID % d prccess % s \ n" % (p. pid, p. name) sys. stdout. flush () time. sleep (3) print "Exiting: % s \ n" % p. name sys. stdout. flush () def main (): p = Process (name = "Daemon", target = daemon) p. daemon = True p. start () if _ name __= = "_ main _": main () time. sleep (1)

Because the main process exits in 1 second, the print "Exiting: % s \ n" % p. name of the daemon cannot be seen.

To view the print "Exiting: % s \ n" % p. name statement output. You can use join to wait for the main process to exit after the daemon exits, that is, p. add p. join () and then time. Sleep (1) Delete

I hope this article will help you with Python programming.

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.