[英]Check if existing process exists - if it does, communicate with it, otherwise create a new one
I'm sure that this question has been asked before, but I couldn't find it.我确定以前有人问过这个问题,但我找不到了。
I have written a Python program which, given a directory, uses a very simple heuristic to determine how to "unpack" the content of it and to what target directory.我写了一个 Python 程序,给定一个目录,它使用一个非常简单的启发式方法来确定如何“解压”它的内容以及到什么目标目录。
This program is executed whenever a new download has completed.只要新的下载完成,就会执行此程序。 If a lot of downloads complete at around the same, I get a lot of processes concurrently unpacking stuff.
如果很多下载几乎同时完成,我会得到很多同时解包的进程。 I'd like to fix this problem by rewriting large portions of the program to only unpack one directory at a time.
我想通过重写程序的大部分来一次只解压一个目录来解决这个问题。
So to achieve this, I figured I'd use a "lock/PID" file which contains the PID of any currently executing program.因此,为了实现这一点,我想我会使用一个“锁/PID”文件,其中包含任何当前正在执行的程序的 PID。 If the lock/PID file exists, the newly spawn processes should simply send something along the lines of
("queue", "D:/some/directory")
to the existing process and have that process unpack that target when it's done with its current unpacking.如果锁/PID 文件存在,新生成的进程应该简单地向现有进程发送类似
("queue", "D:/some/directory")
的内容,并让该进程在完成其目标后解压缩该目标当前开箱。
How would I achieve this in Python?我将如何在 Python 中实现这一目标? This must work on Windows systems, but ideally on GNU/Linux as well.
这必须适用于 Windows 系统,但理想情况下也适用于 GNU/Linux。
So since you're already using something like the lockfile by Ben Finney所以既然你已经在使用像Ben Finney 的 lockfile这样的东西
example:例子:
from lockfile.pidlockfile import PIDLockFile
lock = PIDLockFile('somefile')
lock.acquire(timeout=3600)
#do stuff
lock.release()
You might want to communicate with a running daemon you should have the daemon listen to some socket, then from a spawned process send data to the socket.您可能想要与正在运行的守护进程通信,您应该让守护进程监听某个套接字,然后从生成的进程将数据发送到套接字。 (f.ex a udp socket)
(例如 udp 套接字)
so in the daemon:所以在守护进程中:
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()
on the client:在客户端:
import socket
hostname = 'localhost'
port = 12368
message = "hello"
sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
sock.sendto( message, (hostname, port) )
you can use 'localhost' for the hostname if you're running all of this on the same machine.如果您在同一台机器上运行所有这些,则可以使用“localhost”作为主机名。
On the other hand, using the multiprocess pipes instead of sockets might be the proper way, but I've got no experience in them yet.另一方面,使用多进程管道而不是 sockets 可能是正确的方法,但我还没有使用它们的经验。 This setup has the added benefit of being able to run the server and client on a different machine.
这种设置的额外好处是能够在不同的机器上运行服务器和客户端。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.