那么,让我们开始吧。
1) PYRO - Python Remote Objects - 允许您在不同脚本之间共享对象的库
2) python-daemon - 处理我们脚本的守护进程 - 分离和其他作者称之为“表现良好的守护进程”的东西
有时这个库运行良好,但不能一起使用:(我制作了演示它的脚本(所有三个变体):
from daemon import runner
import Pyro4
import sys
import time
Pyro4.config.HMAC_KEY='topsecret'
class ADaemon():
def __init__(self,pid_file,handler):
self.stdin_path = '/dev/null'
self.stdout_path = '/dev/tty'
self.stderr_path = '/dev/tty'
self.pidfile_path = pid_file
self.pidfile_timeout = 5
self.handler=handler
def run(self):
print "Ready."
self.handler()
class GreetingMaker(object):
def get_fortune(self, name):
return "Hello, {0}. Here is your fortune message:\n" \
"Behold the warranty -- the bold print giveth and the fine print taketh away.".format(name)
greeting_maker=GreetingMaker()
daemon=Pyro4.Daemon() # make a Pyro daemon
uri=daemon.register(greeting_maker) # register the greeting object as a Pyro object
variant=sys.argv[1] #both|pyro|(start|stop) #daemon
if variant == 'both':
print 'Pyro & Daemon example'
print "Pyro is ready. Server uri =", uri # print the uri so we can use it in the client later
sys.argv[1]='start'#dont worry about 'zombi' daemons - daemon dont start in this case:( no apocalipsys:(
runner.DaemonRunner(ADaemon('/tmp/tmp.pid',daemon.requestLoop)).do_action()
elif variant=='pyro':
print 'Pyro example'
print "Pyro is ready. Server uri =", uri # print the uri so we can use it in the client later
daemon.requestLoop()
elif variant=='start' or variant=='stop':
print 'Daemon example'
def testf():
while True:
print 'daemon works fine'
time.sleep(3)
runner.DaemonRunner(ADaemon('/tmp/tmp.pid',testf)).do_action()
else:
print 'usage: both|pyro|(start|stop) #daemon'
火焰兵 4.14-1 守护进程 1.5.5
错误:
argon@vprime:~/py/pyro$ python for_SO.py both
Pyro & Daemon example
Pyro is ready. Server uri = PYRO:obj_9cfa8b5197b4439a988763978a0473e5@localhost:33997
started with pid 23868
Ready.
Traceback (most recent call last):
File "for_SO.py", line 35, in <module>
runner.DaemonRunner(ADaemon('/tmp/tmp.pid',daemon.requestLoop)).do_action()
File "/usr/lib/python2.7/dist-packages/daemon/runner.py", line 186, in do_action
func(self)
File "/usr/lib/python2.7/dist-packages/daemon/runner.py", line 131, in _start
self.app.run()
File "for_SO.py", line 18, in run
self.handler()
File "/usr/local/lib/python2.7/dist-packages/Pyro4-4.14-py2.7.egg/Pyro4/core.py", line 834, in requestLoop
self.transportServer.loop(loopCondition=condition)
File "/usr/local/lib/python2.7/dist-packages/Pyro4-4.14-py2.7.egg/Pyro4/socketserver/threadpoolserver.py", line 130, in loop
self.events([self.sock])
File "/usr/local/lib/python2.7/dist-packages/Pyro4-4.14-py2.7.egg/Pyro4/socketserver/threadpoolserver.py", line 153, in events
csock, caddr=self.sock.accept()
File "/usr/lib/python2.7/socket.py", line 202, in accept
sock, addr = self._sock.accept()
socket.error: [Errno 9] Bad file descriptor