Skip to main content
Tweeted twitter.com/StackCodeReview/status/1610335199551684611
edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
deleted 5 characters in body
Source Link

I want to run all the coroutines from the list in a pool of constant size and would like to ask if this is the right way how to achieve it. Is there any built in solution for this problem? I have not found any.

def subprocess_pool(coroutines, pool_size=3):
    loop = asyncio.get_event_loop()
    finished = 0
    event_finished = asyncio.Event()
    
    def done(future):
        nonlocal finished
        finished += 1
        event_finished.set()

    @asyncio.coroutine 
    def scheduler(coroutines):
        num = len(coroutines)
        finished = 0
        started = 0
        while finished<num:
            if coroutines and (started - finished) < pool_size:
                cor = coroutines.pop(0)
                task = loop.create_task(cor) 
                task.add_done_callback(done)
                started += 1
            else:
                yield from event_finished.wait()
                event_finished.clear()
                finished += 1
        return True

    loop.run_until_complete(scheduler(coroutines))
    loop.close()
    

I want to run all the coroutines from the list in a pool of constant size and would like to ask if this is the right way how to achieve it. Is there any built in solution for this problem? I have not found any.

def subprocess_pool(coroutines, pool_size=3):
    loop = asyncio.get_event_loop()
    finished = 0
    event_finished = asyncio.Event()
    
    def done(future):
        nonlocal finished
        finished += 1
        event_finished.set()

    @asyncio.coroutine 
    def scheduler(coroutines):
        num = len(coroutines)
        started = 0
        while finished<num:
            if coroutines and (started - finished) < pool_size:
                cor = coroutines.pop(0)
                task = loop.create_task(cor) 
                task.add_done_callback(done)
                started += 1
            else:
                yield from event_finished.wait()
                event_finished.clear()
        return True

    loop.run_until_complete(scheduler(coroutines))
    loop.close()

I want to run all the coroutines from the list in a pool of constant size and would like to ask if this is the right way how to achieve it. Is there any built in solution for this problem? I have not found any.

def subprocess_pool(coroutines, pool_size=3):
    loop = asyncio.get_event_loop()
    event_finished = asyncio.Event()
    
    def done(future):
        event_finished.set()

    @asyncio.coroutine 
    def scheduler(coroutines):
        num = len(coroutines)
        finished = 0
        started = 0
        while finished<num:
            if coroutines and (started - finished) < pool_size:
                cor = coroutines.pop(0)
                task = loop.create_task(cor) 
                task.add_done_callback(done)
                started += 1
            else:
                yield from event_finished.wait()
                event_finished.clear()
                finished += 1
        return True

    loop.run_until_complete(scheduler(coroutines))
    loop.close()
    
Source Link

Execute coroutines in pool

I want to run all the coroutines from the list in a pool of constant size and would like to ask if this is the right way how to achieve it. Is there any built in solution for this problem? I have not found any.

def subprocess_pool(coroutines, pool_size=3):
    loop = asyncio.get_event_loop()
    finished = 0
    event_finished = asyncio.Event()
    
    def done(future):
        nonlocal finished
        finished += 1
        event_finished.set()

    @asyncio.coroutine 
    def scheduler(coroutines):
        num = len(coroutines)
        started = 0
        while finished<num:
            if coroutines and (started - finished) < pool_size:
                cor = coroutines.pop(0)
                task = loop.create_task(cor) 
                task.add_done_callback(done)
                started += 1
            else:
                yield from event_finished.wait()
                event_finished.clear()
        return True

    loop.run_until_complete(scheduler(coroutines))
    loop.close()