D
D
DVoropaev2019-08-13 11:22:23
linux
DVoropaev, 2019-08-13 11:22:23

How to "not wait" for input from the user in python?

I want to do something like this:

while True:
    while (s = input()):
        ...
        do_smthing1
        ...
    ...
    do_smthing2
    ...

If the user entered something, then process what he entered (do_smthing1) until the lines on the input run out
. If the user did not enter anything, then do not wait, but do do_smthing2

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-08-13
@DVoropaev

from threading import Thread

def worker():
    while True:
        do_smthing2()


Thread(target=worker).start()

while True:
    s = input()
    do_smthing1()

If do_smthing2 only needs to be done when do_smthing1 is not done, a lock can be used to synchronize threads.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question