queue =  async def funcA(): r = random.randint(0, 1000) i = 0 for i in range(10000000): pass print(r) queue.append(r) asyncio.create_task(funcb(r)) async def funcb(r): i = 0 for i in range(100000000): pass print(r, "XX") queue.pop() async def main(): for i in range(10): await funcA() print(queue) if __name__ == "__main__": asyncio.run(main())
How do I make
funcb() run simultaneously? In this code
funcb() runs only after all calls of
funcA() are done executing. I want
funcB() to run concurrently with
funcA() (on same or different threads). Here if
funcA() runs infinitely, then
funcB() would never run.
Asyncio is a so-called non-preemptive task scheduler.
This means, it cannot switch back and forth between tasks on its own, it requires the function it is calling to actively give up (=yield) the priority.
As your function is not asynchronous (= doesn’t have any commands in it that would wait for something with the
await keyword), it just runs to completion before yielding back to the asyncio scheduler.
The way you described your problem sounds like it isn’t really suitable for asynchronous programming, but instead requires multithreading or even multiprocessing (if you desire to get a speedup from the ‘simultaneous’ computing).