Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'skip:[ 20': 0.04; 'none:': 0.07; 'generators': 0.09; 'try:': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'concurrent': 0.16; 'set()': 0.16; 'subject: \n ': 0.16; 'subject:tasks': 0.16; 'timeout': 0.16; 'import': 0.22; 'accepted.': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'pending': 0.26; 'task': 0.26; 'header:In-Reply-To:1': 0.27; 'raise': 0.29; 'message- id:@mail.gmail.com': 0.30; 'work:': 0.31; 'lists': 0.32; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'except': 0.35; 'something': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'false': 0.36; 'yield': 0.36; 'done': 0.36; 'subject:?': 0.36; 'hi,': 0.36; 'similar': 0.36; 'should': 0.36; 'anything': 0.39; 'skip:a 30': 0.61; 'to:addr:gmail.com': 0.65; 'pool?': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=vcZIGTZFqIeWjmlzd1md7rn1/e5atXcOlbajsHkDug4=; b=AiChfJUJ3t06FoMHTSb31LN8ZtnxkGJMGxah+9tOLR5o2tEpv1Y+qvrCyHsvMFWdon SVwNHgnsutYW+e6qRcyBeisu6VL3ea0rYNACTpO0IEDjT5xYLyICt2UmQgJYLJ3HhAbD ZDwO9cuTuxF/lzmJ6iDYk/K3qSQE6WC++rFHisPdiRLs6/aEczjvVeHl1OZk3+5nVWt6 F1L8sKtC0dvU+NOoMGHdR5OhGTyf7UmGIIPR2hyXrFIpXhfe4zqb2nXwhmZmSeAtmF0i 27moz5vhq+dM1HR5uXDHIuSp0+a4hlfHnEdFoldCBuZokPMtDPMjf+w7xF4vCb+WSgYl rHaA== X-Received: by 10.229.106.136 with SMTP id x8mr52012969qco.15.1405552162582; Wed, 16 Jul 2014 16:09:22 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Maxime Steisel Date: Thu, 17 Jul 2014 01:09:02 +0200 Subject: Re: Anything better than asyncio.as_completed() and asyncio.wait() to manage execution of large amount of tasks? To: Valery Khamenya Content-Type: text/plain; charset=UTF-8 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 57 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1405552164 news.xs4all.nl 2945 [2001:888:2000:d::a6]:54503 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:74595 2014-07-15 14:20 GMT+02:00 Valery Khamenya : > Hi, > > both asyncio.as_completed() and asyncio.wait() work with lists only. No > generators are accepted. Are there anything similar to those functions that > pulls Tasks/Futures/coroutines one-by-one and processes them in a limited > task pool? Something like this (adapted from as_completed) should do the work: import asyncio from concurrent import futures def parallelize(tasks, *, loop=None, max_workers=5, timeout=None): loop = loop if loop is not None else asyncio.get_event_loop() workers = [] pending = set() done = asyncio.Queue(maxsize=max_workers) exhausted = False @asyncio.coroutine def _worker(): nonlocal exhausted while not exhausted: try: t = next(tasks) pending.add(t) yield from t yield from done.put(t) pending.remove(t) except StopIteration: exhausted = True def _on_timeout(): for f in workers: f.cancel() workers.clear() #Wake up _wait_for_one() done.put_nowait(None) @asyncio.coroutine def _wait_for_one(): f = yield from done.get() if f is None: raise futures.TimeoutError() return f.result() workers = [asyncio.async(_worker()) for i in range(max_workers)] if workers and timeout is not None: timeout_handle = loop.call_later(timeout, _on_timeout) while not exhausted or pending or not done.empty(): yield _wait_for_one() timeout_handle.cancel()