Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.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.025 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'url:pipermail': 0.05; '"""': 0.07; 'args': 0.07; 'modified': 0.07; 'python3': 0.07; 'already.': 0.09; 'executed': 0.09; 'instances.': 0.09; 'cc:addr :python-list': 0.11; 'def': 0.12; '16:01': 0.16; 'crawling': 0.16; 'flow.': 0.16; 'modification': 0.16; 'proposed': 0.22; 'cc:addr:python.org': 0.22; 'initial': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'equivalent': 0.26; 'order.': 0.26; 'task': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; "we'd": 0.29; 'network.': 0.30; 'message-id:@mail.gmail.com': 0.30; 'page.': 0.31; 'quite': 0.32; 'url:python': 0.33; 'subject:the': 0.34; "i'd": 0.34; 'could': 0.34; 'subject:with': 0.35; 'except': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'leads': 0.36; 'yield': 0.36; 'url:org': 0.36; 'starting': 0.37; 'tasks': 0.38; 'url:mail': 0.40; 'future': 0.60; 'skip:a 30': 0.61; 'completed': 0.61; 'new': 0.61; 'here:': 0.62; 'kind': 0.63; 'our': 0.64; 'map': 0.64; 'to:addr:gmail.com': 0.65; 'natural': 0.68; 'flood': 0.68; 'results': 0.69; 'naturally.': 0.84; 'out- of': 0.84; 'progression.': 0.84; 'subject:&': 0.84; 'thing,': 0.91 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=vSx0u41IRQIAdJdFzz+gF+7Jr4ZoKI5Hn0ofF+1rJ20=; b=rVkS9moYA9NWy+3yGe3VnDDyQYdRUJwX0He2AH4Q8USmwPQTHhdt38Yv5zEPROraRh gXAlTLclAGoi1KQ5QDd4os7TyiEZC0BwOgA8b/CT5NsunQAkGC3iM+MFRDaIN6ZJEv2w A/hKdwOFWCgPZy+k3BBzKQxanc9COiG3dOtMAvoJLiDsIdPBDRCj48DwWPbloL5tboIc nHtJmr47+PMhn3FjIrvhQ1fbjV2hH4fKuD5ntEsJSeS13+5mK2Z545cK0Ev8Vw5UfrwL SFSzXYXsnZV61q7fFXB44riVmDWFYzWwOzOeHXo1xguk/MJUDZGP/XmqA5tod0ZFsqyI Jq5g== X-Received: by 10.224.20.200 with SMTP id g8mr19223947qab.88.1407344639065; Wed, 06 Aug 2014 10:03:59 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Maxime Steisel Date: Wed, 6 Aug 2014 19:03:38 +0200 Subject: Re: asyncio with map&reduce flavor and without flooding the event loop 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: 1407345106 news.xs4all.nl 2901 [2001:888:2000:d::a6]:45367 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:75806 2014-08-03 16:01 GMT+02:00 Valery Khamenya : > Hi all > > [snip] > > Consider a task like crawling the web starting from some web-sites. Each > site leads to generation of new downloading tasks in exponential(!) > progression. However we don't want neither to flood the event loop nor to > overload our network. We'd like to control the task flow. This is what I > achieve well with modification of nice Maxime's solution proposed here: > https://mail.python.org/pipermail/python-list/2014-July/675048.html > > Well, but I'd need as well a very natural thing, kind of map() & reduce() or > functools.reduce() if we are on python3 already. That is, I'd need to call a > "summarizing" function for all the downloading tasks completed on links from > a page. This is where i fail :( Hi Valery, With the modified as_completed, you can write map and reduce primitives quite naturally. It could look like that: ======== def async_map(corofunc, *iterables): """ Equivalent to map(corofunc, *iterables) except that corofunc must be a coroutine function and is executed asynchronously. This is not a coroutine, just a normal generator yielding Task instances. """ for args in zip(*iterables): yield asyncio.async(corofunc(*args)) @asyncio.coroutine def async_reduce(corofunc, futures, initial=0): """ Equivalent to functools.reduce(corofunc, [f.result() for f in futures]) except that corofunc must be a coroutine function and future results can be evaluated out-of order. This function is a coroutine. """ result = initial for f in as_completed(futures, max_workers=50): new_value = (yield from f) result = (yield from corofunc(result, new_value)) return result ======= Best, Maxime