python multiprocess daemon,pythonmultiprocess,The process’


The process’s daemon flag, a Boolean value. This must be set before start() iscalled.

The initial value is inherited from the creating process.

When a process exits, it attempts to terminate all of its daemonic childprocesses.

Note that a daemonic process is not allowed to create child processes.Otherwise a daemonic process would leave its children orphaned if it getsterminated when its parent process exits. Additionally, these are not Unixdaemons or services, they are normal processes that will be terminated (andnot joined) if non-dameonic processes have exited.

# encoding=utf8'''Created on 2013-3-22@author: corleone'''from multiprocessing import Processimport timefrom uuid import uuid4class TProcess(Process):    def __init__(self):        Process.__init__(self)    def run(self):        while 1:            time.sleep(1)            with open(r'd:\\tprocess', u'a') as f:                f.write(u'%s %s \\n' % (self.name,uuid4()))if __name__ == '__main__':#===============================================================================# cmd:main process print '...' after sleep 3 second , and then stop , tprocess stop # eclipse:main process print '...' after sleep 3 second , and then stop , tprocess stop #===============================================================================#    try:#        t = TProcess()#        t.daemon = True#        t.start()#        time.sleep(3)#        print u'...'#    except Exception as e:#        print u'xxx'#        raise e#===============================================================================# cmd:main process print ... , but not stop and tprocess still running# stop main process manually , tprocess stop too , it's the same when t.join() is uncomment# eclipse:main process print ... , but not stop and tprocess still running# stop main process manually , tprocess still running#===============================================================================    try:        t = TProcess()        t.daemon = False        t.start()        time.sleep(3)        print u'...'#        t.join()    except Exception as e:        print u'xxx'        raise e#===============================================================================# cmd:both running,when stop main process manually then tprocess stop # eclipse:both running,when stop main process, tprocess still running  #===============================================================================#    try:#        t = TProcess()#        t.daemon = False#        t.start()#        t.join()#        print u'to sleep '#        time.sleep(3)#        print u'sleep finished'#        print u'...'#    except Exception as e:#        print u'xxx'#        raise e#===============================================================================# cmd : both running , when stop main process stop then tprocess stop # eclipse : both running ,main process stop , but tprocess still running  #===============================================================================#    try:#        t = TProcess()#        t.daemon = True#        t.start()#        t.join()#        print u'to sleep '#        time.sleep(3)#        print u'sleep finished'#        print u'...'#    except Exception as e:#        print u'xxx'#        raise e#===============================================================================# so in my opinion # daemon means be monitored by the create process or thread passivity# rather than initiative#===============================================================================#该片段来自于http://byrx.net

评论关闭