Python - 禁用多个 py 脚本实例并将它们的参数传递给主实例 [英] Python - Disable multiple py script instances and pass their args to main instance
问题描述
例如,我的 py 脚本已经有一个实例在运行,当我使用 args 触发另一个实例时,不是允许新实例运行,而是将其 args 传递给主实例或使其主实例知道args 这样它就可以对 args 做任何需要做的事情.这样的事情可能吗?
For example my py script already has one instance running and when I fire another instance with args, instead of allowing the new instance to run, make it pass its args to the main instance or make it so the main instance awares of the args so it can do whatever it needs to do with the args. Is something like this possible?
推荐答案
是的,这样的事情是可能的.我不确定是否可以可靠地检测应用程序的一个实例(进程)是否已经在运行,但一种可能的方法是为程序保留一个 TCP 端口.启动的第一个应用程序将侦听该端口并充当服务器.由于只有进程可以同时侦听同一个端口,因此所有后续进程都将无法创建侦听套接字,从而可以充当客户端.
Yes, something like this is possible. I'm not sure if it's possible to reliably detect if one instance (process) of the application is already running, but one possible way to do this is to reserve a TCP port for the program. The first application that starts will listen on that port and act like the server. Since only process can listen at the same port at the same time, all following processes will fail to create a listen socket and could then act like a client.
您可以使用 multiprocessing.connection
来实现这一点.以下是演示该概念的工作示例:
You could use a multiprocessing.connection
to implement this. The following is a working example to demonstrate the concept:
import multiprocessing.connection
import sys
SERVER_HOST = 'localhost'
SERVER_PORT = 6000
g_total = 0
def process_numbers(numbers):
global g_total
for number in numbers:
g_total += number
print('Adding number %d. Total is now %d.' % (number, g_total))
def server_main(listener, numbers):
process_numbers(numbers)
print('Starting server.')
while True:
client = listener.accept()
numbers = list(client.recv())
client.close()
if not numbers:
break
process_numbers(numbers)
def client_main(numbers):
client = multiprocessing.connection.Client((SERVER_HOST, SERVER_PORT), 'AF_INET')
print('Starting client.')
client.send(numbers)
def main():
numbers = map(int, sys.argv[1:])
try:
listener = multiprocessing.connection.Listener((SERVER_HOST, SERVER_PORT), 'AF_INET')
except OSError:
client_main(numbers)
else:
server_main(listener, numbers)
if __name__ == '__main__':
main()
假设您已将上述代码保存为 main.py
,调用 py main.py 1 2 3
将打印以下输出:
Assuming you have saved the above code as main.py
, calling py main.py 1 2 3
will print the following output:
Adding number 1. Total is now 1.
Adding number 2. Total is now 3.
Adding number 3. Total is now 6.
Starting server.
应用程序将继续运行.现在打开第二个终端并运行 py main.py 4 5 6
.该脚本只会打印:
The application will keep running. Now open a second terminal and run py main.py 4 5 6
. The script will only print:
Starting client.
但是,您的第一个应用程序(充当服务器)现在将打印:
However, your first application (acting as the server) will now print:
Adding number 4. Total is now 10.
Adding number 5. Total is now 15.
Adding number 6. Total is now 21.
如果你重复它打印的命令:
If you repeat the command it prints:
Adding number 4. Total is now 25.
Adding number 5. Total is now 30.
Adding number 6. Total is now 36.
等等.您可以通过不带参数调用客户端来退出服务器.
and so on. You can quit the server by calling the client without arguments.
注意:这个答案是为 Python 3.7 编写并测试的,因为 Python 2.x 已经死了.但是 multiprocessing.connection
也存在在 Python 2.7 中,所以我认为代码应该也可以在 Python 2.7 中运行(可能需要稍作修改).
NOTE: This answer was written for and tested in Python 3.7, since Python 2.x is already dead. But the multiprocessing.connection
also exists in Python 2.7, so I think the code should probably work in Python 2.7 as well (possibly with minor modifications).
这篇关于Python - 禁用多个 py 脚本实例并将它们的参数传递给主实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!