python多进程单例_Python守护进程和脚本单例运行

2、类实现#!/usr/bin/env python#coding: utf-8

#python模拟linux的守护进程

importsys, os, time, atexit, stringfrom signal importSIGTERMclassDaemon:def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):#需要获取调试信息,改为stdin='/dev/stdin', stdout='/dev/stdout', stderr='/dev/stderr',以root身份运行。

self.stdin =stdin

self.stdout=stdout

self.stderr=stderr

self.pidfile=pidfiledef_daemonize(self):try:

pid= os.fork() #第一次fork,生成子进程,脱离父进程

if pid >0:

sys.exit(0)#退出主进程

exceptOSError, e:

sys.stderr.write('fork #1 failed: %d (%s)\n' %(e.errno, e.strerror))

sys.exit(1)

os.chdir("/") #修改工作目录

os.setsid() #设置新的会话连接

os.umask(0) #重新设置文件创建权限

try:

pid= os.fork() #第二次fork,禁止进程打开终端

if pid >0:

sys.exit(0)exceptOSError, e:

sys.stderr.write('fork #2 failed: %d (%s)\n' %(e.errno, e.strerror))

sys.exit(1)#重定向文件描述符

sys.stdout.flush()

sys.stderr.flush()

si= file(self.stdin, 'r')

so= file(self.stdout, 'a+')

se= file(self.stderr, 'a+', 0)

os.dup2(si.fileno(), sys.stdin.fileno())

os.dup2(so.fileno(), sys.stdout.fileno())

os.dup2(se.fileno(), sys.stderr.fileno())#注册退出函数,根据文件pid判断是否存在进程

atexit.register(self.delpid)

pid=str(os.getpid())

file(self.pidfile,'w+').write('%s\n' %pid)defdelpid(self):

os.remove(self.pidfile)defstart(self):#检查pid文件是否存在以探测是否存在进程

try:

pf= file(self.pidfile,'r')

pid=int(pf.read().strip())

pf.close()exceptIOError:

pid=Noneifpid:

message= 'pidfile %s already exist. Daemon already running!\n'sys.stderr.write(message%self.pidfile)

sys.exit(1)#启动监控

self._daemonize()

self._run()defstop(self):#从pid文件中获取pid

try:

pf= file(self.pidfile,'r')

pid=int(pf.read().strip())

pf.close()exceptIOError:

pid=Noneif not pid: #重启不报错

message = 'pidfile %s does not exist. Daemon not running!\n'sys.stderr.write(message%self.pidfile)return

#杀进程

try:while 1:

os.kill(pid, SIGTERM)

time.sleep(0.1)#os.system('hadoop-daemon.sh stop datanode')

#os.system('hadoop-daemon.sh stop tasktracker')

#os.remove(self.pidfile)

exceptOSError, err:

err=str(err)if err.find('No such process') >0:ifos.path.exists(self.pidfile):

os.remove(self.pidfile)else:printstr(err)

sys.exit(1)defrestart(self):

self.stop()

self.start()def_run(self):"""run your fun"""

whileTrue:#fp=open('/tmp/result','a+')

#fp.write('Hello World\n')

sys.stdout.write('%s:hello world\n' %(time.ctime(),))

sys.stdout.flush()

time.sleep(2)if __name__ == '__main__':

daemon= Daemon('/tmp/watch_process.pid', stdout = '/tmp/watch_stdout.log')if len(sys.argv) == 2:if 'start' == sys.argv[1]:

daemon.start()elif 'stop' == sys.argv[1]:

daemon.stop()elif 'restart' == sys.argv[1]:

daemon.restart()else:print 'unknown command'sys.exit(2)

sys.exit(0)else:print 'usage: %s start|stop|restart' %sys.argv[0]

sys.exit(2)

运行结果:

可以参考:http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/,它是当Daemon设计成一个模板,在其他文件中from daemon importDaemon,然后定义子类,重写run()方法实现自己的功能。classMyDaemon(Daemon):defrun(self):whileTrue:

fp=open('/tmp/run.log','a+')

fp.write('Hello World\n')

time.sleep(1)

不足:信号处理signal.signal(signal.SIGTERM, cleanup_handler)暂时没有安装,注册程序退出时的回调函数delpid()没有被调用。

然后,再写个shell命令,加入开机启动服务,每隔2秒检测守护进程是否启动,若没有启动则启动,自动监控恢复程序。#/bin/sh

whiletrue

do

count=`ps -ef | grep "daemonclass.py" | grep -v "grep"`if [ "$?" != "0"]; then

daemonclass.py start

fi

sleep2done

三、python保证只能运行一个脚本实例1、打开文件本身加锁#!/usr/bin/env python#coding: utf-8

importfcntl, sys, time, os

pidfile=0defApplicationInstance():globalpidfile

pidfile= open(os.path.realpath(__file__), "r")try:

fcntl.flock(pidfile, fcntl.LOCK_EX| fcntl.LOCK_NB) #创建一个排他锁,并且所被锁住其他进程不会阻塞

except:print "another instance is running..."sys.exit(1)if __name__ == "__main__":

ApplicationInstance()whileTrue:print 'running...'time.sleep(1)

注意:open()参数不能使用w,否则会覆盖本身文件;pidfile必须声明为全局变量,否则局部变量生命周期结束,文件描述符会因引用计数为0被系统回收(若整个函数写在主函数中,则不需要定义成global)。2、打开自定义文件并加锁#!/usr/bin/env python#coding: utf-8

importfcntl, sys, time

pidfile=0defApplicationInstance():globalpidfile

pidfile= open("instance.pid", "w")try:

fcntl.lockf(pidfile, fcntl.LOCK_EX| fcntl.LOCK_NB) #创建一个排他锁,并且所被锁住其他进程不会阻塞

exceptIOError:print "another instance is running..."sys.exit(0)if __name__ == "__main__":

ApplicationInstance()whileTrue:print 'running...'time.sleep(1)3、检测文件中PID#!/usr/bin/env python#coding: utf-8

importtime, os, sysimportsignal

pidfile= '/tmp/process.pid'

defsig_handler(sig, frame):ifos.path.exists(pidfile):

os.remove(pidfile)

sys.exit(0)defApplicationInstance():

signal.signal(signal.SIGTERM, sig_handler)

signal.signal(signal.SIGINT, sig_handler)

signal.signal(signal.SIGQUIT, sig_handler)try:

pf= file(pidfile, 'r')

pid=int(pf.read().strip())

pf.close()exceptIOError:

pid=Noneifpid:

sys.stdout.write('instance is running...\n')

sys.exit(0)

file(pidfile,'w+').write('%s\n' %os.getpid())if __name__ == "__main__":

ApplicationInstance()whileTrue:print 'running...'time.sleep(1)4、检测特定文件夹或文件#!/usr/bin/env python#coding: utf-8

importtime, commands, signal, sysdefsig_handler(sig, frame):if os.path.exists("/tmp/test"):

os.rmdir("/tmp/test")

sys.exit(0)defApplicationInstance():

signal.signal(signal.SIGTERM, sig_handler)

signal.signal(signal.SIGINT, sig_handler)

signal.signal(signal.SIGQUIT, sig_handler)if commands.getstatusoutput("mkdir /tmp/test")[0]:print "instance is running..."sys.exit(0)if __name__ == "__main__":

ApplicationInstance()whileTrue:print 'running...'time.sleep(1)

也可以检测某一个特定的文件,判断文件是否存在:importosimportos.pathimporttime#class used to handle one application instance mechanism

classApplicationInstance:#specify the file used to save the application instance pid

def __init__( self, pid_file ):

self.pid_file=pid_file

self.check()

self.startApplication()#check if the current application is already running

defcheck( self ):#check if the pidfile exists

if notos.path.isfile( self.pid_file ):return

#read the pid from the file

pid =0try:

file= open( self.pid_file, 'rt')

data=file.read()

file.close()

pid=int( data )except:pass

#check if the process with specified by pid exists

if 0 ==pid:return

try:

os.kill( pid, 0 )#this will raise an exception if the pid is not valid

except:return

#exit the application

print "The application is already running..."exit(0)#exit raise an exception so don't put it in a try/except block

#called when the single instance starts to save it's pid

defstartApplication( self ):

file= open( self.pid_file, 'wt')

file.write( str( os.getpid() ) )

file.close()#called when the single instance exit ( remove pid file )

defexitApplication( self ):try:

os.remove( self.pid_file )except:pass

if __name__ == '__main__':#create application instance

appInstance = ApplicationInstance( '/tmp/myapp.pid')#do something here

print "Start MyApp"time.sleep(5) #sleep 5 seconds

print "End MyApp"

#remove pid file

appInstance.exitApplication()

上述os.kill( pid, 0 )用于检测一个为pid的进程是否还活着,若该pid的进程已经停止则抛出异常,若正在运行则不发送kill信号。5、socket监听一个特定端口#!/usr/bin/env python#coding: utf-8

importsocket, time, sysdefApplicationInstance():try:globals

s=socket.socket()

host=socket.gethostname()

s.bind((host,60123))except:print "instance is running..."sys.exit(0)if __name__ == "__main__":

ApplicationInstance()whileTrue:print 'running...'time.sleep(1)

可以将该函数使用装饰器实现,便于重用(效果与上述相同):#!/usr/bin/env python#coding: utf-8

importsocket, time, sysimportfunctools#使用装饰器实现

defApplicationInstance(func):

@functools.wraps(func)def fun(*args,**kwargs):importsockettry:globals

s=socket.socket()

host=socket.gethostname()

s.bind((host,60123))except:print('already has an instance...')returnNonereturn func(*args,**kwargs)returnfun

@ApplicationInstancedefmain():whileTrue:print 'running...'time.sleep(1)if __name__ == "__main__":

main()

四、总结

(1)守护进程和单脚本运行在实际应用中比较重要,方法也比较多,可选择合适的来进行修改,可以将它们做成一个单独的类或模板,然后子类化实现自定义。

(2)daemon监控进程自动恢复避免了nohup和&的使用,并配合shell脚本可以省去很多不定时启动挂掉服务器的麻烦。

(3)若有更好的设计和想法,可随时留言,在此先感谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值