Receive user input without interrupting program execution

I'm working on a program (a bot actually, you can see the code here if you like) that has an infinite loop running all the time.

I'm using asyncio

as part of the code, so I first tried to create another routine that got input and checked for commands. However, it doesn't seem to work.

What I want to do is be able to issue program commands without interrupting execution, for example using input()

. Preferably, it will have a character >

and a string that remains at the bottom of the screen with the program output above it and allows data to be entered.

Is it possible to do this with asyncio

or do I need to look into my multithreaded program or something?

EDIT . I thought that maybe I could use a GUI ncurses

that has an input field at the bottom and all the bot output above the input field. Is it possible?

+3


source to share


2 answers


You should be able to use asyncio as StdIn is another stream you can choose from ...



+1


source


from threading import Thread
import shlex


def endless_job():
    while True:
        pass


job = Thread(target=endless_job)
job.start()

while True:
    user_input = input('> ')
    print(shlex.split(user_input))

      

The shlex module helps you parse the user entered command line :)

If you need to pass arguments to the endless_job function, you can do something like:



job = Thread(target=endless_job, args=(1,'a'), kwargs={'a': 1, 'b': 2})

      

where args

and kwargs

stand for positional and named arguments, respectively.

-1


source







All Articles