简体   繁体   English

将参数传递给正在运行的 python 脚本

[英]Passing arguments to a running python script

I have a script running on my raspberry, these script is started from a command from an php page.我有一个在我的树莓派上运行的脚本,这些脚本是从 php 页面的命令启动的。 I've multiple if stetements, now I would like to pass new arguments to the script whithout stopping it.我有多个 if 语句,现在我想在不停止脚本的情况下将新参数传递给脚本。 I found lots of information by passing arguments to the python script, but not if its possible while the svpcript is already running to pass new arguments.我通过将参数传递给 python 脚本找到了很多信息,但如果可能的话,当 svpcript 已经在运行以传递新参数时,我发现了很多信息。 Thanks in advance!提前致谢!

The best option for me is to use a configuration file input for your script.对我来说最好的选择是为您的脚本使用配置文件输入。 Some simple yaml will do.一些简单的 yaml 就可以了。 Then in a separate thread you must observe the hash of the file, if it gets changed that means somebody has updated your file and you must re/adjust your inputs.然后在一个单独的线程中,您必须观察文件的哈希值,如果它被更改,则意味着有人更新了您的文件,您必须重新/调整您的输入。

Basically you have that constant observer running all the time.基本上,您一直在运行那个恒定的观察者。

You need some sort of IPC mechanism really.您确实需要某种 IPC 机制。 As you are executing/updating the script from a PHP application, I'd suggest you'll look into something like ZeroMQ which supports both Python and PHP, and will allow you to do a quick and dirtyPub/Sub implementation.当您从 PHP 应用程序执行/更新脚本时,我建议您研究一下 ZeroMQ 之类的东西,它支持 Python 和 PHP,并允许您执行快速而肮脏的Pub/Sub实现。

The basic idea is, treat your python script as a subscriber to messages coming from the PHP application which publishes them as and when needed.基本思想是,将您的 python 脚本视为来自 PHP 应用程序的消息的订阅者,该应用程序在需要时发布它们。 To achieve this, you'll want to start your python "script" once and leave it running in the background, listening for messages on ZeroMQ.为了实现这一点,您需要启动一次您的python“脚本”并让它在后台运行,监听ZeroMQ上的消息。 Something like this should get you going像这样的事情应该让你走

import zmq

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555")

while True:
    #  Wait for next message from from your PHP application
    message = socket.recv()
    print("Recieved a message: %s" % message)

    #  Here you should do the work you need to do in your script

    #  Once you are done, tell the PHP application you are done
    socket.send(b"Done and dusted")

Then, in your PHP application, you can use something like the following to send a message to your Python service然后,在您的 PHP 应用程序中,您可以使用以下内容向您的 Python 服务发送消息

$context = new ZMQContext();

//  Socket to talk to server

$requester = new ZMQSocket($context, ZMQ::SOCKET_REQ);
$requester->connect("tcp://localhost:5555");

$requester->send("ALL THE PARAMS TO SEND YOU YOUR PYTHON SCRIPT");
$reply = $requester->recv();

Note, I found the above examples using a quick google search (and amended slightly for educational purposes), but they aren't tested, and purely meant to get you started.请注意,我使用快速谷歌搜索找到了上述示例(并出于教育目的稍作修改),但它们未经测试,纯粹是为了让您入门。 For more information, visit ZeroMQ and php-zmq有关更多信息,请访问ZeroMQphp-zmq

Have fun.玩得开心。

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

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