简体   繁体   English

Python-将参数传递给已经运行的进程

[英]Python - Passing arguments to an already running process

first of all, i´m pretty new to Python. 首先,我是Python的新手。 I also searched for a solution, but i guess the usual approach (subprocess.popen) won´t work in my case. 我也搜索了一个解决方案,但我想通常的方法(subprocess.popen)在我的情况下不起作用。

I have to pass arguments to a listener in an already running python script without starting the script over and over again. 我必须将参数传递给已经运行的python脚本中的侦听器,而不必一遍又一遍地启动脚本。 There is an example how to pass a message to a lcd-screen: 有一个示例如何将消息传递到LCD屏幕:

function printMsgText(message_text)
local f = io.popen("home/test/show_message.py '" .. message_text .. "'")
end

This lua-script above defines the process which gets called everytime a message is recieved. 上面的lua脚本定义了每次收到消息时都会调用的过程。 The called process (show_message.py) looks like that: 调用的进程(show_message.py)如下所示:

import sys
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.show_message(sys.argv[1])

I need something similar, except that there is another script running in the backround, so show_message.py is not the final process but needs to pass the argument/message to another, already running script. 我需要类似的东西,除了在后台运行另一个脚本之外,所以show_message.py不是最终过程,但需要将参数/消息传递给另一个已经运行的脚本。 My idea was to just let show_message.py print the message to the console and use sys.argv in the main process aswell but i´ma little afraid that it could get messy. 我的想法是只让show_message.py将消息打印到控制台,并在主要过程中也使用sys.argv,但我有点害怕它会变得凌乱。

Is there any easy way to do this? 有没有简单的方法可以做到这一点? Kind regards 亲切的问候

Edit: 编辑:

The main script is controlling a stepper-motor. 主要脚本是控制步进电机。 Based on the user input, the motor drives a pre-defined number of steps. 根据用户输入,电动机将驱动预定义的步数。 The part of the script waiting for the user-input looks like this: 脚本中等待用户输入的部分如下所示:

while wait:
            user_position = input("Where do you wanna go? (0, 1, 2, back): ")
            wait = False
            #  Console output
            print("Position: " + str(user_position))

            if user_position == "0":
                stepper.set_target_position(position_zero)
                wait = True
            elif user_position == "1":
                stepper.set_target_position(position_one
                wait = True
            elif user_position == "2":
                stepper.set_target_position(position_two)
                wait = True
            elif user_position == "back":
                break

Now i need to pass the desired position via a web-application which is designed the way i described above (eg calling a lua-script every time a variable/argument is passed) and not via the console. 现在,我需要通过网络应用程序传递所需的位置,该应用程序的设计方式如上所述(例如,每次传递变量/参数时调用lua脚本),而不是通过控制台。

Once a process is running it won't re-evaluate its command line arguments. 进程运行后,将不会重新评估其命令行参数。 You need other ways to communicate with it. 您需要其他方式与之通信。 This is colloquially called inter-process communication (IPC), and there are several ways to achieve it. 俗称这是进程间通信 (IPC),有几种方法可以实现。 Here are a few: 这里有一些:

  • Files
  • Pipes (on platforms that support them) 管道(在支持管道的平台上)
  • Shared memory 共享内存
  • Socket communication 套接字通讯
  • Message passing 讯息传递
  • RPC RPC

Probably the most approachable way is standard streams (STDIN, STDOUT) as provided by eg subprocess.popen() which you mentioned. 可能最容易接近的方法是您提到的例如subprocess.popen()提供的标准流(STDIN,STDOUT)。 But this requires a parent-child relation between the communicating processes. 但这需要通信过程之间的父子关系。 Maybe you can go this route. 也许你可以走这条路。

Another way for Python, if you want to avoid parent-child relations, which I have made good experiences with is Pyro . 如果要避免亲子关系,Python的另一种方法是Pyro ,我对此有很好的经验。 It was easy to use and worked well, albeit with some performance penalty. 尽管性能有所下降,但它易于使用且运行良好。 It is also a very "infrastructure-ish" way to go about it, both processes have to be coded to use it. 这也是一种非常“基础架构式”的处理方式,必须对这两个过程进行编码才能使用它。

You can use some sort of messaging library that will allow you to communicate between processes. 您可以使用某种消息传递库,使您可以在进程之间进行通信。 ZeroMQ is a good option, and has python bindings. ZeroMQ是一个不错的选择,并且具有python绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM