- # encoding=utf8
- '''
- Created on 2013-3-22
- @author: corleone
- '''
- from multiprocessing import Process
- import time
- from uuid import uuid4
- class 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://www.codesnippet.cn/detail/060920135692.html
来源: http://www.codesnippet.cn/detail/060920135692.html