I have two scripts:
one continuosly comunicates with the gps module and refresh the gps time variable.
the second one ask me when I want to view the gps time and prints it on the console.
How can I retrieve the gps_time variable of the first script (while it running in background) from the second script?
You can do some socket programming.
Maybe you can get some idea from here:
Interprocess communication in Python
Official docs:
https://docs.python.org/3.10/library/socket.html
Related
I currently have a simple HTML with three input text boxes and a button running on Node.js. I am able to send values from the HTML page to the python script as arguments when executing the script (sys.argv) through Node.js as a child process.
The python script keeps generating values. Right now, I simply print those values, but can I grab these values and send them back to the webpage every second until the script stops running after about 3 minutes? If yes, how can I grab them?
I want to use Node.js because I want to use the package pdfmake (https://www.npmjs.com/package/pdfmake) from npm to generate reports of the same.
If you're already executing the Python script as a child process, simply capture the STDOUT stream from the process.
https://nodejs.org/api/child_process.html
https://nodejs.org/api/child_process.html#child_process_subprocess_stdout
I do not have comment privileges yet and this may not be an exact answer but have you looked into websockets? It looks like you should be able to emit the data from the python code to the port that you may be hosting your web page on.
I'm having some problems with communicating between 2 Python scripts.
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. 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. (4 per Raspberry)
You can enter on the webpage the machine and how long the machine should be activated.
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.
So far everything is working great.
I also have a small 2-line LCD screen for each Raspberry Pi.
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.
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.
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.
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.
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.
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).
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:
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}
I am creating a test automation which uses an application without any interfaces. However, The application calls a batch script when it changes modes, and I am therefore am able to catch the mode transitions.
What I want to do is to get the batch script to give an input to my python script (I have a state machine running in python) during runtime. Such that I can monitor the state of the application with python instead of the batch file.
I am using a similar state machine to the one of Karn Saheb:
https://dev.to/karn/building-a-simple-state-machine-in-python
However, instead of changing states statically like:
device.on_event('event')
I want the python script to do something similar to:
while(True):
device.on_event(input()) # where the input is passed from the batch script:
REM state.bat
set CurrentState=%1
"magic code to pass CurrentState to python input()" %CurrentState%
I see that a solution would be to start the python script from the batch file every time it is called with the "event" and then save the current event in another file upon termination of the python script... But I want to avoid such handling and rather evaluate this during runtime.
Thank you in advance!
A reasonably portable way of doing this without ugly polling on temporary files is to use a socket: have the main process listen and have the batch file(s) start a small program that connects to the server and writes a message.
There are security considerations here: you can start by listening only to the loopback interface, with further authentication if the local machine should not be trusted.
If you have more than one of these processes, or if you need to handle the child dying before it issues its next report, you’ll have to use threads or something like select to unify the news from different input channels (e.g., waiting on the child to exit vs. waiting on news from the next batch file).
I have a script that continually runs and accepts data (For those that are familiar and if it helps, it is connected to EMDR - https://eve-market-data-relay.readthedocs.org).
Inside the script I have debugging built in so that I can see how much data is currently in the queue for the threads to process, however this is built to be used with just printing to the console. What I would like to do is be able to either run the same script with an additional option or a totally different script that would return the current queue count without having to enable debug.
Is there a way to do this could someone please point me in the direction of the documentation/libaries that I need to research?
There are many ways to solve this; two that come to mind:
You can write the queue count to a k/v store (like memcache or redis) and then have another script read that for you and do whatever other actions required.
You can create a specific logger for your informational output (like the queue length) and set it to log somewhere else other than the console. For example, you could use it to send you an email or log to an external service, etc. See the logging cookbook for examples.
I'm trying to share Data between 2 python scripts that are running at the exact same time.
Here is an image to explain how im doing this.
http://i859.photobucket.com/albums/ab153/coreystj/Problem_zps9acdd726.png
This is all done in python. The problem im having is that whenever i Have new information to append to ClientSend.txt file(from script 2) it sometimes doesnt register for script 1 to send it to the server. I was thinking maybe its because when i append and write the same file at the exact same time, i end up by accidentally deleting the information before sending it to the server!
How Can i avoid accidentally deleting the contents before sending it. If there is no solution, how else can i share information between two scripts without using socket or any sort of tcp connection.
Script 2 is actually Blender FYI. So multiprocessing, and threading is NOT an option for script two. It simply doesn't support those modules.
You could use the actor model described at http://en.wikipedia.org/wiki/Actor_model