python实现的守护进程(Daemon)用法实例

yipeiwu_com5年前Python基础

本文实例讲述了python实现的守护进程(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程序设计有所帮助。

相关文章

关于pip的安装,更新,卸载模块以及使用方法(详解)

在Python的学习过程中,肯定会遇到很多安装模块的地方,可以使用easy_install安装,但是easy_install相对于pip而言,最大的缺陷就是它所安装的模块是不能够卸载的,...

Python处理XML格式数据的方法详解

本文实例讲述了Python处理XML格式数据的方法。分享给大家供大家参考,具体如下: 这里的操作是基于Python3平台。 在使用Python处理XML的问题上,首先遇到的是编码问题。...

浅谈Python_Openpyxl使用(最全总结)

Python_Openpyxl 1. 安装 pip install openpyxl 2. 打开文件 ① 创建 from openpyxl import Workboo...

Python基于time模块求程序运行时间的方法

Python基于time模块求程序运行时间的方法

本文实例讲述了Python基于time模块求程序运行时间的方法。分享给大家供大家参考,具体如下: 要记录程序的运行时间可以利用Unix系统中,1970.1.1到现在的时间的毫秒数,这个时...

Python break语句详解

Python break语句详解

Python break语句,就像在C语言中,打破了最小封闭for或while循环。break语句用来终止循环语句,即循环条件没有False条件或者序列还没被完全递归完,也会停止执行循环...