How to execute a loop while waiting for input?

5

I want to know if you can execute a loopback while waiting for an input () input, and immediately after receiving the input the for loop or while starts processing she immediately.

something like:

imputs = []
parar = false
def faca_algo(i):
    #fazendo algo


inputs.append(input("diga algo: ") # <--- essa linha permaneçe ativa

while not parar: #    <---- esse loop continua sendo executado
    faca_algo(inputs)
    
asked by anonymous 31.12.2016 / 13:42

1 answer

4

Yes it is possible, but not with a single Thread , since programs are sequences of instructions.

If I understand what you want, this is a simpler type of Produtor/Consumidor problem (since I do not think I have a memory limitation). You will need to work with at least two threads and a shared memory area (a vetor , or lista , etc. [will treat here as lista ]) between the two threads.

While thread produtora reads the data from the terminal and stores it in lista to thread consumidora removes the next element from the list and performs the desired processing.

However, working with shared memory areas causes some problems, as in thread consumidora tries to get an element and lista is empty. To solve this problem you will have to use semáforos , locks and etc.

For a more in-depth study I suggest reading these articles.

This is a better understanding of the Producer and Consumer problem

  

link

This one for python thread synchronization mechanisms

  

link

    
31.12.2016 / 21:10