0

All programs which I see in tutorials are console and code is executed from first line to the last line and if there is while everything starts from the first line. Is there any way for console programs to change their execution way, due to some event, like e.g. key press or some event in code? The best example of what I want to do is router CLI. Where can I find such examples?

def main(): 
    while(True):
        initial_setup() #choose IPs to monitor
        while(True):
            do_some_work() # do monitor the IPs

I need some listener in the secons while which detects keypresses and then I go to initial setup, meanwhile do_some_work works and only after I finish adittional changes in initial_setup do_some_work restarts.

Sorry I am noob and not very good in explaining probaly because English is not native for me. The best example from real life I can name is CLI of router, you can setup intreface and meanwhile router do routing in the background.

Code for Sergio S:

import threading
import time

def hello():
    while(True):
        print("Hello")
        time.sleep(2)

def hi():
    while(True):
       print("hi")
       time.sleep(2)
def press_key():
    a=input()
    a=False
    return a
def circle():
    MrBoolean=True
    while(MrBoolean):
        thr=[]
        thr.append(threading.Thread(target=hello))
        thr.append(threading.Thread(target=hi))
        thr.append(threading.Thread(target=press_key))
        for i in thr:
            i.start()
        for i in thr:
            i.join()
        mrBoolean=thr[3] 
 def main():
    while(True):
        circle()

main()
Artiom Kozyrev
  • 3,526
  • 2
  • 13
  • 31
  • 1
    Yes, all procedural programming languages can do that. You will have to be more specific, and show some python code, to get a full answer and avoid being closed as"unclear" or "too broad". – cdarke Sep 04 '18 at 06:53
  • 1
    Try a library such as python Click to get started with CLI applications in python. – José María Sep 04 '18 at 06:57
  • @cdarke it is really a general question, since I can't find any examples of such programs. Do python have any way to change commands execution order due to some user activity? – Artiom Kozyrev Sep 04 '18 at 07:51
  • 1
    There is the normal way of doing things as in any 3GL, functions, `if` statements, loops, etc. You can store function references in a dictionary (hash table) and make calls depending on input data (a call table). Function names are dynamic (can change at runtime), so it is very flexible. – cdarke Sep 04 '18 at 14:32
  • @cdarke I would give code example what I need to do – Artiom Kozyrev Sep 04 '18 at 16:32
  • @cdarke I would give code example what I need to do 'main()' then do 'initial_setup()' then do 'while(True): somefunction()' in parallel layer with somefuction there should be some listener function which detect keypress, if keypress is detected you go to initial_setup and add new data for somefunction. It will be ideally if somefuction works until I do changes in initial setup and then restarts with new settings. – Artiom Kozyrev Sep 04 '18 at 16:39

2 Answers2

1
whats_typed = input('Say Aah:')
if whats_typed.strip() == 'Aah':
    print('Thanks!')
else:
    print('Whoops. Your input was:', whats_typed)

The above changes what is executed depending on user input when the program is run.

Paddy3118
  • 4,704
  • 27
  • 38
  • I would give code example what I need to do 'main()' then do 'initial_setup()' then do 'while(True): somefunction()' in parallel layer with somefuction there should be some listener function which detect keypress, if keypress is detected you go to initial_setup and add new data for somefunction. It will be ideally if somefuction works until I do changes in initial setup and then restarts with new settings. – Artiom Kozyrev Sep 04 '18 at 16:40
1

From your description, it seems you're searching for something called multithreading: while one part of the application does one thing, the other does something else. See these other questions for more details: How to use threading in Python? , How to stop a looping thread in Python?

Sergio S.
  • 116
  • 5
  • Thank you Sergio, I added a bunch of code, let me know how I can finisg two left infinite threads and return to the beggining of while loop in main()? – Artiom Kozyrev Sep 05 '18 at 18:42
  • 1
    I believe this question will answer yours, Artiom: https://stackoverflow.com/questions/18018033/how-to-stop-a-looping-thread-in-python . – Sergio S. Sep 05 '18 at 18:59