Python Forum
How to add coroutine to a running event loop?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to add coroutine to a running event loop?
#1
Python 3.5

Initially, I launched several coroutine in ordered stages (which in the coroutine contain their tasks):
async def _create_workers(self):
    scheme = get_scheme()
    for stage in scheme.get_next_stage():
        await asyncio.gather(*stage)

loop = asyncio.get_event_loop()
loop.run_until_complete(self._create_workers())
But in the process of work, it became known that one of the coroutine doesn't have time to done with its tasks, and I want to add copies of it. I analyze the load from one of the coroutine ('main'- coroutine), which adds data for processing. But if I create new task I have to wait for the new coroutine to complete:
new_task = asyncio.ensure_future(slow_coro())
await new_task
But I don’t want to stop the execution flow 'main'- coroutine for waiting! I just want to add it to the current stage and so that the processing stage will finish only when the new coroutine is also completed.

How I can do it?
Reply
#2
Don't use await until you want to stop adding things to the event loop. Whatever you want to be running together, add to the event loop, and then after they're all added is when you await the various tasks. Here's an example:

import asyncio

async def worker():
    print("Starting work")
    await asyncio.sleep(5)
    print("Work complete")

async def other_worker():
    print("Starting other worker")
    await asyncio.sleep(2)
    print("Other worker complete")

async def third_worker():
    print("Starting third worker")
    await asyncio.sleep(1)
    print("Third worker complete")

async def main():
    print("Starting workers")
    tasks = []
    tasks.append(asyncio.ensure_future(worker()))
    tasks.append(asyncio.ensure_future(other_worker()))

    print("First two workers added to the event loop.")
    for task in tasks:
        await task

    print("Adding third worker to the event loop.")
    await third_worker()
    # in this case, this is the same thing
    await asyncio.ensure_future(third_worker())

asyncio.run(main())
Output:
Starting workers First two workers added to the event loop. Starting work Starting other worker Other worker complete Work complete Adding third worker to the event loop. Starting third worker Third worker complete Starting third worker Third worker complete
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  loop running indefinitely shantanu97 4 313 Jun-14-2021, 04:52 PM
Last Post: deanhystad
  Running loop at specific frequency mdsousa 3 788 Apr-21-2021, 11:22 AM
Last Post: jefsummers
  RuntimeError: This event loop is already running newbie2019 2 4,834 Sep-30-2020, 06:59 PM
Last Post: forest44
  Running function from parent module which has a loop in it. ta2909i 1 1,080 Nov-18-2019, 07:04 PM
Last Post: Gribouillis
  How to get coroutine from a task, and an object from a coroutine? AlekseyPython 2 1,292 Mar-23-2019, 01:41 PM
Last Post: AlekseyPython
Exclamation Error: There is no current event loop in thread 'MainThread' AlekseyPython 5 8,194 Feb-21-2019, 04:37 AM
Last Post: AlekseyPython
  action on MQTT while long loop is running runboy 4 3,151 Oct-05-2018, 11:57 PM
Last Post: runboy
  Outer loop not running ted_gress 2 1,494 Aug-25-2018, 07:56 AM
Last Post: volcano63
  Running Class methods in a loop and updating variables. ujjwalrathod007 3 2,943 Oct-05-2016, 07:11 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020