那么既然你已经使用类似的lockfile by Ben Finney
例如:
from lockfile.pidlockfile import PIDLockFile
lock = PIDLockFile('somefile')
lock.acquire(timeout=3600)
#do stuff
lock.release()
您可能想要与正在运行的守护进程进行通信,您应该让该守护进程侦听某个套接字,然后从衍生进程发送数据到插座。 (f.ex 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' 的,如果你正在运行的所有在同一台机器上。
另一方面,使用multiprocess pipes而不是套接字可能是正确的方法,但我还没有经验。 此设置具有能够在另一台计算机上运行服务器和客户端的附加好处。
等待,下载完成时执行什么程序 - 您的Python脚本或脚本执行的内容?你如何安排新的过程开始? – 2012-04-11 08:37:42
我的Python程序在下载完成时执行。它唯一的参数是下载目录的路径。 – damd 2012-04-11 08:39:02
是什么原因导致您的Python程序在下载完成时执行? – 2012-04-11 08:40:15