python实现的守护进程(Daemon)用法实例
本文实例讲述了python实现的守护进程(Daemon)用法。分享给大家供大家参考。具体如下:
defcreateDaemon():
"'Funzionechecreaundemonepereseguireundeterminatoprogramma…"'
importos
#create-fork1
try:
ifos.fork()>0:os._exit(0)#exitfather…
exceptOSError,error:
print'fork#1failed:%d(%s)'%(error.errno,error.strerror)
os._exit(1)
#itseparatesthesonfromthefather
os.chdir('/')
os.setsid()
os.umask(0)
#create-fork2
try:
pid=os.fork()
ifpid>0:
print'DaemonPID%d'%pid
os._exit(0)
exceptOSError,error:
print'fork#2failed:%d(%s)'%(error.errno,error.strerror)
os._exit(1)
funzioneDemo()#functiondemo
deffunzioneDemo():
importtime
fd=open('/tmp/demone.log','w')
whileTrue:
fd.write(time.ctime()+'\n')
fd.flush()
time.sleep(2)
fd.close()
if__name__=='__main__':
createDaemon()
希望本文所述对大家的Python程序设计有所帮助。
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短