简体   繁体   中英

Python asyncio two tasks and only one is running

I am new to python and struggling to understand why my coroutine is not working.

In the current code, the only one job is running and another is always stays idle. Why?

class Worker:    
    def job1_sync(self):
        count = 0
        while True:
            print('JOB A:', count)
            count = count + 1

    def job2_sync(self):
        count = 0
        while True:
            print('JOB B:', count)
            count = count + 1

    async def job1(self):
        await self.job1_sync()

    async def job2(self):
        await self.job2_sync()

    worker = Worker()
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    loop.run_until_complete(asyncio.gather(worker.job1(), worker.job2()))

Asyncio does not do multi-tasking or multithreading. What it does is it schedules tasks within one thread, using a cooperative model.

That is, the event loop runs again when the current task await s something that will "block", and only then it schedules another task. Under the hood, async functions are coroutines, and calls to await make the corouting yield to the event loop, which resumes it a later point, when awaited condition arises.

Here you never await anything, so job1 never relinquishes control, so the event loop never has a chance to distribute computing power to other tasks.

Now if your job was to actually relinquish control, say by triggering a delay, then your code would work:

async def job1_sync(self):      # note the async : only async functions can await
    count = 0
    while True:
        print('JOB A:', count)
        count = count + 1
        await asyncio.sleep(1)  # main even loop gets control

TLDR: asyncio is useful for what it says: doing stuff asynchronously, allowing other tasks to make progress while current task waits for something. Nothing runs in parallel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM