#主函数
if __name__ == '__main__':
#以下注释部分为采用守护进程方案启动程序,若采用此方案,将注释打开,并将最后一行(server())注释掉
logger = logging.getLogger(config.logger_name)
if config.server_daemon:
try:
pid = os.fork()
if pid > 0:
# exit first parent
os._exit(0)
except OSError, e:
logger.error("fork #1 failed: %d (%s)" % (e.errno, e.strerror))
os._exit(0)
# decouple from parent environment
#os.chdir("/")
os.setsid()
os.umask(0)
# do second fork
while(True):
try:
pid = os.fork()
logger.info( "pid: %d",pid)
if pid == 0:
serve()
if pid > 0:
logger.info("Server PID %d, Daemon PID: %d" % (pid, os.getpid()))
os.wait()
logger.info("child exit, restarting")
time.sleep(3)
dir_path = os.path.dirname(os.path.realpath(__file__))
fs = glob.glob(dir_path+'/core.*')
for f in fs:
os.remove(f)
except OSError, e:
logger.error("fork #2 failed: %d (%s)" % (e.errno, e.strerror))
os._exit(0)
else:
serve()
python守护程序
最新推荐文章于 2024-04-30 22:59:39 发布