User avatar
sapnho
Posts: 223
Joined: Sun Sep 14, 2014 3:49 pm
Location: Frankfurt am Main
Contact: Website

How to change the value of a variable that is being used in a looped Python script in terminal window?

Mon Aug 20, 2018 6:17 pm

I apologize if this is a trivial question but even after searching I couldn't find an answer to it.

I have a endlessly looped Python script. The script has a different behaviour depending on the value of certain values within this script. Let's say, it changes an image every x seconds.

Is it possible to change the value of this variable through a terminal window command so that the looped Python script reacts to it?
www.TheDigitalPictureFrame.com
Build a really great digital picture frame with the Raspberry Pi.
With lots of great tips from this forum.

hippy
Posts: 6775
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: How to change the value of a variable that is being used in a looped Python script in terminal window?

Mon Aug 20, 2018 7:05 pm

I don't think there's any way to alter a variable's value directly inside a Python program unless you were perhaps running in some sort of debugging environment. But you could do it programmatically.

The Python program could simply monitor for keys pressed when running and take that as a command or value to use.

If the program is running in the background or you want to control it from some other terminal window, you could have another program or use a command line which writes something to a file which your running program can read and acts upon.

There may be better tricks such as 'named pipes' which might be worth investigating.

User avatar
sapnho
Posts: 223
Joined: Sun Sep 14, 2014 3:49 pm
Location: Frankfurt am Main
Contact: Website

Re: How to change the value of a variable that is being used in a looped Python script in terminal window?

Mon Aug 20, 2018 7:21 pm

Thanks! It's a headless system, so no keyboard attached.

I suppose the writing would work with pickle.dump and pickle.read. The named pipe command looks a bit more complicated for my modest Python knowledge..
www.TheDigitalPictureFrame.com
Build a really great digital picture frame with the Raspberry Pi.
With lots of great tips from this forum.

hippy
Posts: 6775
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: How to change the value of a variable that is being used in a looped Python script in terminal window?

Mon Aug 20, 2018 7:26 pm

Here's some fun with named pipes. create "NamedPipe.py" ...

Code: Select all

#!/user/bin/python

import time

def ReadNamedPipe(pipeName):
  with open(pipeName,"r") as f:
    while True:
      s = f.readline()
      if s:
        s = s.strip()
        print "You said : "+ s
        if s.upper() == "QUIT":
          return
      else:
        time.sleep(0.1)

ReadNamedPipe("./mypipe")
Then run ...

Code: Select all

[email protected]:~$ mkfifo mypipe
[email protected]:~$ python NamedPipe.py &
[1] 9472
[email protected]:~$ echo "hello" > ./mypipe
You said : hello
[email protected]:~$ echo "quit" > ./mypipe
[email protected]:~$ You said : quit

[1]+  Done                    python NamedPipe.py
[email protected]:~$
Had to press return after "quit" was sent so I guess it needs more work done.

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: How to change the value of a variable that is being used in a looped Python script in terminal window?

Mon Aug 20, 2018 9:42 pm

I've always found named pipes to be more trouble than they are worth. The problem is that since they are actual, real files in the file system, you have to be careful to remove them when you're done. This makes things complicated - especially during the debugging process.

Instead, I've always used TCPIP. It works just like a pipe and is "self-cleaning".

I've not done this in Python, but it is straightforward in C, using "select". I'm assuming Python has some way of accessing the same underlying system calls as you can do in C.

Note: The reason I mention "select" is that you usually want to be able to do this without blocking. That is, you have a loop that checks periodically with "select" to see if there's anything on the TCPIP connection; else you can do your regular processing.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

User avatar
sapnho
Posts: 223
Joined: Sun Sep 14, 2014 3:49 pm
Location: Frankfurt am Main
Contact: Website

Re: How to change the value of a variable that is being used in a looped Python script in terminal window?

Tue Aug 21, 2018 3:59 am

Thanks to both of you. To play it safe, I guess, I'll just take the variable-in-a-file route. But the other approaches do sound interesting.
www.TheDigitalPictureFrame.com
Build a really great digital picture frame with the Raspberry Pi.
With lots of great tips from this forum.

User avatar
PeterO
Posts: 5468
Joined: Sun Jul 22, 2012 4:14 pm

Re: How to change the value of a variable that is being used in a looped Python script in terminal window?

Tue Aug 21, 2018 8:25 am

n67 wrote:
Mon Aug 20, 2018 9:42 pm
Note: The reason I mention "select" is that you usually want to be able to do this without blocking. That is, you have a loop that checks periodically with "select" to see if there's anything on the TCPIP connection; else you can do your regular processing.
poll is a better choice these days...
https://stackoverflow.com/questions/970 ... and-select
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Return to “Python”