因此,既然您已经在使用Ben Finney 的 lockfile之类的东西
例子:
from lockfile.pidlockfile import PIDLockFile
lock = PIDLockFile('somefile')
lock.acquire(timeout=3600)
#do stuff
lock.release()
您可能希望与正在运行的守护程序通信,您应该让守护程序侦听某个套接字,然后从生成的进程向套接字发送数据。(例如 udp 套接字)
所以在守护进程中:
import socket
import traceback
import Queue
import threading
import sys
hostname = 'localhost'
port = 12368
#your lockfile magick here
# if you want one script to run each time, put client code here instead of seperate script
#if already running somehwere:
#message = "hello"
#sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
#sock.sendto( message, (hostname, port) )
#sys.exit(0)
que = Queue.Queue(0)
socket_ = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
socket_.bind((hostname, port))
class MyThread(threading.Thread):
def run(self):
while True: #maybe add some timeout here, so this process closes after a while
# but only stop if que.empty()
message = que.get()
print "handling message: %s" % message
#handle message
t = MyThread()
t.start()
while True:
try:
#blocking call
message, _ = socket_.recvfrom(8192) #buffer size
print "received message: %s" % message
#queue message
que.put(message)
except (KeyboardInterrupt, SystemExit):
raise
except:
traceback.print_exc()
在客户端:
import socket
hostname = 'localhost'
port = 12368
message = "hello"
sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
sock.sendto( message, (hostname, port) )
如果您在同一台机器上运行所有这些,则可以使用“localhost”作为主机名。
另一方面,使用多进程管道而不是套接字可能是正确的方法,但我还没有这方面的经验。此设置的额外好处是能够在不同的机器上运行服务器和客户端。