python實現(xiàn)的守護進程(Daemon)用法實例
更新時間:2015年06月02日 17:12:07 作者:niuniu
這篇文章主要介紹了python實現(xiàn)的守護進程(Daemon)用法,實例分析了Python進程操作的相關(guān)技巧,需要的朋友可以參考下
本文實例講述了python實現(xiàn)的守護進程(Daemon)用法。分享給大家供大家參考。具體如下:
def createDaemon():
"'Funzione che crea un demone per eseguire un determinato programma…"'
import os
# create - fork 1
try:
if os.fork() > 0: os._exit(0) # exit father…
except OSError, error:
print 'fork #1 failed: %d (%s)' % (error.errno, error.strerror)
os._exit(1)
# it separates the son from the father
os.chdir('/')
os.setsid()
os.umask(0)
# create - fork 2
try:
pid = os.fork()
if pid > 0:
print 'Daemon PID %d' % pid
os._exit(0)
except OSError, error:
print 'fork #2 failed: %d (%s)' % (error.errno, error.strerror)
os._exit(1)
funzioneDemo() # function demo
def funzioneDemo():
import time
fd = open('/tmp/demone.log', 'w')
while True:
fd.write(time.ctime()+'\n')
fd.flush()
time.sleep(2)
fd.close()
if __name__ == '__main__':
createDaemon()
希望本文所述對大家的Python程序設(shè)計有所幫助。
相關(guān)文章
使用Python的內(nèi)建模塊collections的教程
這篇文章主要介紹了使用Python的內(nèi)建模塊collections的教程,示例代碼基于Python2.x版本,需要的朋友可以參考下2015-04-04
Python實現(xiàn)Word表格轉(zhuǎn)成Excel表格的示例代碼
這篇文章主要介紹了Python實現(xiàn)Word表格轉(zhuǎn)成Excel表格的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
控制Python浮點數(shù)輸出位數(shù)的操作方法
在python的輸出結(jié)果中,尤其是浮點數(shù)的輸出,當我們需要寫入文本文件時,最好是采用統(tǒng)一的輸出格式,這樣也能夠增強結(jié)果的可讀性,這篇文章主要介紹了控制Python浮點數(shù)輸出位數(shù)的方法,需要的朋友可以參考下2022-04-04
對python-3-print重定向輸出的幾種方法總結(jié)
今天小編就為大家分享一篇對python-3-print重定向輸出的幾種方法總結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05

