那麼既然你已經使用類似的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