Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to get input from console while an infinite loop is running?

I'm trying to write a simple Python IRC client. So far I can read data, and I can send data back to the client if it automated. I'm getting the data in a while True, which means that I cannot enter text while at the same time reading data. How can I enter text in the console, that only gets sent when I press enter, while at the same time running an infinite loop?

Basic code structure:

while True:
    read data
    #here is where I want to write data only if it contains '/r' in it
like image 293
Shef Avatar asked Jun 02 '14 17:06

Shef


People also ask

Can we take input in while loop in Python?

To take user input in a while loop:Use a while loop to iterate until a condition is met. Use the input() function to take user input. If the condition is met, break out of the while loop.

How do you take input while running in Python?

Getting Interactive Python Input in the ShellThe input() command allows you to require a user to enter a string or number while a program is running. The input() method replaced the old raw_input() method that existed in Python v2.

How do you ask for input repeatedly in Python?

How to Ask for Input Again in Python. To ask for user input until they give valid input, use an endless while loop with some error handling. For example, let's create a program that asks the user's age. If the user does not enter an integer, we print an error message and ask again.


2 Answers

Another way to do it involves threads.

import threading

# define a thread which takes input
class InputThread(threading.Thread):
    def __init__(self):
        super(InputThread, self).__init__()
        self.daemon = True
        self.last_user_input = None

    def run(self):
        while True:
            self.last_user_input = input('input something: ')
            # do something based on the user input here
            # alternatively, let main do something with
            # self.last_user_input

# main
it = InputThread()
it.start()
while True:
    # do something  
    # do something with it.last_user_input if you feel like it
like image 176
timgeb Avatar answered Sep 29 '22 17:09

timgeb


What you need is an event loop of some kind.

In Python you have a few options to do that, pick one you like:

  • Twisted https://twistedmatrix.com/trac/
  • Asyncio https://docs.python.org/3/library/asyncio.html
  • gevent http://www.gevent.org/

and so on, there are hundreds of frameworks for this, you could also use any of the GUI frameworks like tkinter or PyQt to get a main event loop.

As comments have said above, you can use threads and a few queues to handle this, or an event based loop, or coroutines or a bunch of other architectures. Depending on your target platforms one or the other might be best. For example on windows the console API is totally different to unix ptys. Especially if you later need stuff like colour output and so on, you might want to ask more specific questions.

like image 33
schlenk Avatar answered Sep 29 '22 16:09

schlenk