0

I'm a beginner in Python, so I'll try to explain the best I can what the problem is. I'm working on a script that, when started, should ping some servers and print their status. After that, it should ask the user for the next step. Options are to ping them again or to exit the script. If the user does not respond within 60 seconds, the script should refresh, ping servers again, and prompt the user with options again. It works fine on the first run, but after the timer calls the ping function again, it does not prompt for the user input. Here is the sample code and the output:

from os import system
from threading import Thread
import time
import os

def auto_refresh():
    time.sleep(60)
    start_script()

def ping():
    print('Pinging servers ...')
    # ping logic
    print('Servers status ...\n')

def dialog():
    print("[1] Start again")
    print("[2] Exit")
    ans=input("What would you like to do?: ")
    if ans == "1":  
        ping()
    elif ans == "2":
        os._exit(0)
    else:
        print('Error: Invalid selection')
        dialog()

def start_script():
    system('cls')
    t = Thread(target=auto_refresh)
    t.start()
    ping()
    dialog()

start_script()

Output on the first run:

Pinging servers ...
Servers status ...

[1] Start again
[2] Exit
What would you like to do?:

Output after 60 seconds have passed:

Pinging servers ...
Servers status ...

[1] Start again
[2] Exit
  • Does this answer your question? [Keyboard input with timeout?](https://stackoverflow.com/questions/1335507/keyboard-input-with-timeout) – buran Jan 26 '22 at 10:17
  • Yes, it did help, thank you so much. I abandon multithreading and used `inputimeout` module instead. Thanks again :) – drmarem Jan 26 '22 at 11:14

0 Answers0