0

I want to be able to perform other functions while the others are not resolved or completed, the problem is that I don't know how to do this, my code is below:

async def bar():
 name = str(input("Name:"))
 print(name)

async def ba():
 print("Test")

async def allfunctions():
 await bar()
 await ba()

asyncio.run(allfunctions())

I wish I could run other functions while others aren't finished, but realize that if you run this code it will wait until you type a username to run the next function, and how can I modify this script to run other functions while the others don't are finished or resolved?

  • Does this answer your question? [Python async: Waiting for stdin input while doing other stuff](https://stackoverflow.com/questions/58454190/python-async-waiting-for-stdin-input-while-doing-other-stuff) – marke Jun 18 '21 at 09:16
  • It's because you are awaiting for the first function to run. See that `await bar()` that's the problem. You need to add each function to the event loop separately. – ARK1375 Jun 18 '21 at 09:22
  • @marke I didn't quite understand the suggested question –  Jun 18 '21 at 09:26
  • @ARK1375 How would this look in practice? –  Jun 18 '21 at 09:27

1 Answers1

0

You could use the threading module and do it like this:

import threading

def get_input():
    foo = input("type something here\n")
    print(foo)
    a = input()

def print_something():
    for i in range(11):
        print(i)

thread1 = threading.Thread(target=get_input)
thread2 = threading.Thread(target=print_something)

thread1.start()
thread2.start()

thread1.join()
thread2.join()

The output of this is:

type something here 0
1
2
3
4
5
6
7
8
9
10
test # my input
test 
Einliterflasche
  • 473
  • 6
  • 18