それでは、始めましょう。
1) PYRO - Python Remote Objects - 別のスクリプト間でオブジェクトを共有できるライブラリ
2) python-daemon - スクリプトのデーモン化を処理します - detach および作成者が「行儀の良いデーモン」と呼ぶその他のもの
一度にこのライブラリはうまく機能しますが、一緒には機能しません:(それを示すスクリプトを作成しました(3つのバリアントすべて):
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'
Pyro 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