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程序设计有所帮助。

相关文章

Python+Redis实现布隆过滤器

布隆过滤器是什么   布隆过滤器(Bloom Filter)是1970年由布隆提出的。它实际上是一个很长的二进制向量和一系列随机映射函数。布隆过滤器可以用于检索一个元素是否在一个集合中。...

python psutil监控进程实例

我就废话不多说了,直接上代码吧! import psutil import subprocess import os from os.path import join,getsize...

python读取LMDB中图像的方法

本文实例为大家分享了python读取LMDB中的图像具体代码,供大家参考,具体内容如下 图像数据写入LMDB之后最好再按照写入的逻辑反向解析写入的图像,如果图像能够被还原则证明写入方式是...

python获取时间及时间格式转换问题实例代码详解

整理总结一下python中最常用的一些时间戳和时间格式的转换 第一部分:获取当前时间和10位13位时间戳 import datetime, time '''获取当前时间''' n =...

Python操作MySQL数据库的两种方式实例分析【pymysql和pandas】

Python操作MySQL数据库的两种方式实例分析【pymysql和pandas】

本文实例讲述了Python操作MySQL数据库的两种方式。分享给大家供大家参考,具体如下: 第一种 使用pymysql 代码如下: import pymysql #打开数据库连接 d...