1

Is it possible to change a value when script is running inside a loop?

import sys

inputValue = 120

def changeValue(value):
 inputValue = value

def main():
 if inputValue == 0:
  print 'Value is'+str(inputValue)

if __name__ == '__main__':
 while True:
  main()

I think to change executing the script python app.py calling the function python app.py changeValue 23 but I dont know how exactly it'd work.

6
  • I don't see why it wouldn't work, but generally using globals is considered bad design. Commented Mar 22, 2019 at 20:56
  • Or a bad practice, rather. Commented Mar 22, 2019 at 20:56
  • What is the best practice to make a test like this? The script have the intention for detecting changes of some value and trigger another functions. Commented Mar 22, 2019 at 21:04
  • Not too familiar with python specific best practices, but you might try an object. Commented Mar 22, 2019 at 21:07
  • do you have any example? Commented Mar 22, 2019 at 21:09

2 Answers 2

2

There are many ways to control a running program, but to a first approximation all of them involve alternating between doing some work and checking for input. A very simple approach would be something like this:

import string

value = 1

def checkChangeValue():
    newValue = input()
    if newValue.isdigit():
        global value
        value = int(newValue)

def main():
    print("Value is {}".format(value))

def loop():
    main()
    checkChangeValue()


if __name__ == '__main__':
    while True:
        loop()

This runs the main function ("do some work") and then checkChangeValue ("check for input") over and over. This may not do what you want, though, because input() waits for you to type something and hit enter, so it will block the execution of main. To get around this you could add threads:

import string
import threading

value = 1

def checkChangeValue():
    global value
    while True:
        newValue = input()
        if newValue.isdigit():
            value = int(newValue)

def main():
    print("Value is {}".format(value))


if __name__ == '__main__':
    threading.Thread(target=checkChangeValue).start()
    while True:
       main()

Either of these methods may work, depending on your needs. For a more sophisticated approach you might look at the pynput library, which lets you interact with your program through keyboard or mouse input.

There are many other different ways of controlling a running program, too! You could communicate with your program through files, pipes, sockets, shared memory, etc. This in general is called "Inter-process communication" or IPC.

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

Comments

1

You have to declare inputValue as a global variable using the global statement inside the changeValue function in order for assignment statements to that variable inside the function to be applied to inputValue as a global variable; otherwise the assignment would be made to inputValue as a variable local to changeValue instead:

def changeValue(value):
    global inputValue
    inputValue = value

Note that any use of a global variable other than for the purpose of read-only application-wide configuration is generally discouraged. You should consider making your global variable a local one and pass it around as a function argument and as a returning value instead, e.g.:

import sys

def changeValue(value):
    return value + 1

def main(value):
    value = changeValue(value)
    print('Value is %d' % value)

if __name__ == '__main__':
    inputValue = int(sys.argv[1])
    while True:
        main(inputValue)

3 Comments

How can I call this arg? python app.py 'number' ? Considering the script is running
You can obtain the first argument to the script with sys.argv[1]. I've updated the example in my answer to reflect that.
I don't think this solution fits the bill: the question is how to control a running program. This lets you start the program with a value but then it will just print that value plus one over and over, and nothing you do from outside will change that.

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.