Example analysis of Python daemon usage

Source: Internet
Author: User
This example describes the Python daemon usage. Share to everyone for your reference. The specific analysis is as follows:

The daemon is capable of running without blocking the main program from exiting. To flag a daemon, you can set the Daemon property 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 will exit after 1 seconds, the daemon's print "exiting:%s\n"% p.name is not visible.

If you want to see the print "exiting:%s\n"% p.name statement output of the daemon, you can take advantage of the join, the main process waits for the daemon to exit before exiting, that is, after P.start () add P.join () and then put time. Sleep (1) Delete

Hopefully 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.