Python Forum
How do you take terminal inputs w/o halting running code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do you take terminal inputs w/o halting running code?
#1
Hi,

I have an application which runs indefinitely (a while True loop until a keyboard interrupt stops the program). Now I want to be able to give some commands to it for debugging and testing purpose while this program is in running state.
Using the input() function halts the running program. Anyway to do this parallely?

Thanks
Reply
#2
The best way to do this may depend on the OS. Linux, Windows, or Mac?
Reply
#3
I'm using Linux
I've been in the mean time trying out some code available online, had a partial success.

My current implementation for this is:
import termios, fcntl, sys, os
def get_char_keyboard_nonblock():
    fd = sys.stdin.fileno()

    oldterm = termios.tcgetattr( fd )
    newattr = termios.tcgetattr( fd )
    newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
    termios.tcsetattr( fd, termios.TCSANOW, newattr )

    oldflags = fcntl.fcntl( fd, fcntl.F_GETFL )
    fcntl.fcntl( fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK )

    c = None

    try:
        c = sys.stdin.read( 1 )
    except IOError:
        pass

    termios.tcsetattr( fd, termios.TCSAFLUSH, oldterm )
    fcntl.fcntl( fd, fcntl.F_SETFL, oldflags )

    return c
Usage:
        data = ""
        while True:
            input_char = get_char_keyboard_nonblock()
            if input_char == '\n':
                print( 'Got', data )
                data = ""
            elif input_char != "":
                data += input_char
            """
            do project related stuff
            """
            # sleep thread
            time.sleep( 0.02 )
This reads the data quite well, but I can't alter the inputs (i.e. backspace doesn't work and basically, as the input is character by character, I can't remove it).
I'm also not keen on using another thread for this as i'm afraid of making it too complicated (It's not for user, just myself)
Reply
#4
You could try executing your code in a separate thread. Or you could try using the python debugger.

Python threads:
https://docs.python.org/3.8/library/threading.html

The pdb debugger:
https://docs.python.org/3.8/library/pdb.html
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  under VS Code: the terminal does not recognize "1+1" as a command Rahzan 4 59 May-01-2026, 01:53 PM
Last Post: Rahzan
Photo Running Brackets code Jojogeno 2 89 Feb-13-2026, 10:30 AM
Last Post: buran
  Returning numeral output in VS code terminal StephCreatesCode 4 217 Jan-26-2026, 03:45 PM
Last Post: DeaD_EyE
  code not running even without errors Azdaghost 2 1,080 Apr-25-2025, 07:35 PM
Last Post: Azdaghost
  python code not running Azdaghost 1 828 Apr-22-2025, 08:44 PM
Last Post: deanhystad
  writing and running code in vscode without saving it akbarza 5 4,895 Mar-03-2025, 08:14 PM
Last Post: Gribouillis
  problem in running a code akbarza 7 3,348 Feb-14-2024, 02:57 PM
Last Post: snippsat
  the order of running code in a decorator function akbarza 2 1,969 Nov-10-2023, 08:09 AM
Last Post: akbarza
  Code freez if more inputs kiko058 2 2,091 Mar-28-2023, 03:52 PM
Last Post: kiko058
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 3,003 Jan-16-2023, 07:38 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020