简体   繁体   English

从 python 脚本通信到已经运行的脚本

[英]Communicating from python script to already running script

I'm having some problems with communicating between 2 Python scripts.我在 2 个 Python 脚本之间进行通信时遇到了一些问题。 I'm fairly new to python and raspberry pi's and i found already multiple methods which after some time to understand the method turned out to useless for me.我对 python 和 raspberry pi 还很陌生,我发现已经有多种方法,经过一段时间的理解,这些方法对我来说毫无用处。 so after some hours spend, i thought it would be better to ask more experienced people.所以花了几个小时后,我认为最好问问更有经验的人。

So i'm working on a project where i'm using a webpage to control multiple machines via a multiple Raspberry Pi.所以我正在做一个项目,我使用网页通过多个 Raspberry Pi 控制多台机器。 (4 per Raspberry) You can enter on the webpage the machine and how long the machine should be activated. (每个 Raspberry 4 个)您可以在网页上输入机器以及机器应该激活多长时间。 The webpage then executes a Python Script with the machine and the time to run as arguments to the raspberry pi and activates the machine for the specified time.然后,网页使用机器执行 Python 脚本,并将运行时间作为 raspberry pi 的参数,并在指定的时间内激活机器。 So far everything is working great.到目前为止,一切都很好。

I also have a small 2-line LCD screen for each Raspberry Pi.我还为每个 Raspberry Pi 配备了一个小型 2 行 LCD 屏幕。 This screen should change every 10 or so seconds and show each machine on the raspberry and the time the machine is still running and then change to the next one.此屏幕应每 10 秒左右更改一次,并显示树莓上的每台机器以及机器仍在运行的时间,然后更改为下一台。

So the script to show every thing on the screen is an endless loop which changes every 10 seconds but i don't know how i should properly get the new running times into a running python script.所以在屏幕上显示所有东西的脚本是一个无限循环,每 10 秒改变一次,但我不知道我应该如何正确地将新的运行时间放入正在运行的 python 脚本中。

I use the values already in the python script to activate the machines so i thought i could somehow send the informations from this script to the endless, already running LCD script.我使用 python 脚本中已有的值来激活机器,所以我想我可以以某种方式将信息从这个脚本发送到无尽的、已经运行的 LCD 脚本。

But the most methods i found are stopping and waiting for a signal from the other script.但是我发现的大多数方法是停止并等待来自其他脚本的信号。 But then it doesn't change every 10 seconds.但随后它不会每 10 秒改变一次。

The only method that i know right now is to save to files and read the files in the other script xD but that isn't very elegant.我现在知道的唯一方法是保存到文件并读取另一个脚本 xD 中的文件,但这不是很优雅。

I'm thankful for every help and advise that i can get.我很感谢我能得到的每一个帮助和建议。

Kiwi猕猴桃

You could use a database (SQLite is a simple file-based DB system, which, at least using Perl, you can put the DB directly into memory).您可以使用数据库(SQLite 是一个简单的基于文件的数据库系统,至少使用 Perl,您可以将数据库直接放入内存中)。

Another way would be to use shared memory, whether via a module, or the file system itself.另一种方法是使用共享内存,无论是通过模块还是文件系统本身。

Here's an example with one Python script writing a data structure to a JSON file to /dev/shm shared memory space, and another script reading the JSON back in as the written data structure:这是一个示例,其中一个 Python 脚本将数据结构写入到/dev/shm共享内存空间的 JSON 文件,另一个脚本将 JSON 作为写入的数据结构读回:

Output script:输出脚本:

import json

file = "/dev/shm/data.json"

data = {
    "pi1_enabled": True,
    "pi1_run_mins": 30,
    "pi2_enabled": False,
    "pi2_run_mins": 30
}

with open(file, "w") as jsonfile:
    json.dump(data, jsonfile)

Input script:输入脚本:

import json

file = "/dev/shm/data.json"

data = json.loads(open(file).read())

print(data)

Output from input script:输入脚本的输出:

{'pi1_run_mins': 30, 'pi1_enabled': True, 'pi2_enabled': False, 'pi2_run_mins': 30}

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

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