2

first of all, i´m pretty new to Python. I also searched for a solution, but i guess the usual approach (subprocess.popen) won´t work in my case.

I have to pass arguments to a listener in an already running python script without starting the script over and over again. There is an example how to pass a message to a lcd-screen:

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. The called process (show_message.py) looks like that:

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. 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´m a little afraid that it could get messy.

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 (e.g. calling a lua-script every time a variable/argument is passed) and not via the console.

0

2 Answers 2

5

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. Here are a few:

  • Files
  • Pipes (on platforms that support them)
  • Shared memory
  • Socket communication
  • Message passing
  • RPC

Probably the most approachable way is standard streams (STDIN, STDOUT) as provided by e.g. subprocess.popen() which you mentioned. 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. 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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks alot.In my understanding, a parent-child relation would cause me some issues since the parent process would get started over and over again everytime i send a message and so would the child, right?
@TheCakeWasALie If I got your description correctly it could work if your web application starts the background process as a child, and then passes incoming messages to it.
I think here is no way i can hold a connection between the web-app and my main script, at least i was told. I can only send commands to my device which is connected over a proprietary protocol. On my device (a RPi) i then can define a script which gets started as soon as a command is recieved. When i send another command, the script is started again. So everytime i change the position, a new instance of my script would get started. So i need my main script running in the backround, controlling the stepper and some kind of "control" script just passing one number/string and exiting afterwards.
@TheCakeWasALie Then you need some IPC. You can start your main script independently (e.g. when the device boots). Every instance of the control script then connects to the main script and forwards the data it received. For the communication use what suits you best, e.g. Pyro, or simply let the main script open a socket it listens to, and the controller scripts open that socket for writing.
Would you please add some more info regarding pipes. I have a running Python script and I want another Python script running some requests. How to implement pipes in this case ?
|
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.