Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3a.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.015 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; "'',": 0.07; 'everybody!': 0.09; 'subject:skip:c 10': 0.09; 'api': 0.11; 'django': 0.11; 'question.': 0.14; 'thread': 0.14; '"here': 0.16; 'concurrency,': 0.16; 'coroutines': 0.16; 'docs.': 0.16; 'expecting': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; "guido's": 0.16; 'lambda': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'possible?': 0.16; 'received:192.168.1.4': 0.16; 'thread,': 0.16; 'threads.': 0.16; 'usage,': 0.16; 'wrote:': 0.18; 'library': 0.18; 'looked': 0.18; 'year,': 0.18; 'app': 0.19; 'module': 0.19; 'memory': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'define': 0.26; 'mention': 0.26; 'switch': 0.26; 'this:': 0.26; 'header:In-Reply- To:1': 0.27; 'correct': 0.29; "i'm": 0.30; 'crash': 0.31; 'interface': 0.32; 'run': 0.32; 'running': 0.33; 'style': 0.33; 'sense': 0.34; "i'd": 0.34; 'could': 0.34; 'skip:u 20': 0.35; 'something': 0.35; 'there': 0.35; 'really': 0.36; 'i.e.': 0.36; 'doing': 0.36; 'implement': 0.38; 'tasks': 0.38; 'to:addr:python- list': 0.38; 'files': 0.38; 'little': 0.38; 'does': 0.39; "couldn't": 0.39; 'heard': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'skip:u 10': 0.60; 'eventually': 0.60; 'skip:a 30': 0.61; 'simple': 0.61; 'hearing': 0.63; 'more': 0.64; 'kept': 0.65; 'between': 0.67; 'saw': 0.77; 'lack': 0.78; '100': 0.79; 'url:jpg': 0.83; 'built,': 0.84; 'done."': 0.84; 'mature': 0.84; 'subject:Using': 0.84; 'tricky': 0.84; 'clueless': 0.91; 'these.': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=Dv3Y+QP+ c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=u9EReRu7m0cA:10 a=QL9Wq4oc51IA:10 a=bN1huPkv6lYA:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=1XWaLZrsAAAA:8 a=PpAtxSF5-MHjLQyf2IkA:9 a=RXgZGi78_rpMH5Z9:21 a=FPetdFAMrUB4v8oG:21 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett:2500 Date: Tue, 12 Aug 2014 19:43:15 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Using asyncio workers in a `concurrent.futures` interface References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1407869004 news.xs4all.nl 2909 [2001:888:2000:d::a6]:56843 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:76131 On 2014-08-12 18:02, cool-RR wrote: > Hello everybody! I have a question. > > I have a Django app running on Heroku. I need to run about 100 worker > threads there to do uploads/downloads simultaneously. A Heroku Dyno > has only 512MB of memory, so I'm reluctant to run 100 worker threads. > (I've had Dynos crash from lack of memory when using 6 threads > before.) > > I heard that the asyncio module is mature and ready for usage, and I > was happy because I kept hearing about it in the last year, and I saw > Guido's lecture about it. If I understand correctly it would let me > run multiple uploads and downloads efficiently in one thread, which > would conserve more resources than using threads. (Please correct me > if I'm wrong.) > > Now, I am a little clueless about the whole way it's built, using > coroutines and tricky usage of `yield from`. I figured that since > this is eventually a library for concurrency, i.e. doing many tasks > at the same time, there will be an API in a style of "Here are 100 > tasks for you to do concurrently, let me know when they're done." > > I looked at the asyncio documentation page and saw that it does > mention futures and executors, which is my favorite interface for > doing concurrency. I was happy and I skimmed the docs. But, I > couldn't find a simple way to use these. I don't want to learn how to > define coroutines and use `yield from` to switch between them. (I use > `yield from` regularly and fully understand how it works, I just > don't write my programs that way.) > > What I'm expecting is something like this: > > download_file = lambda url: requests.get(url).content > urls = ['http://google.com/file1.jpg', 'http://google.com/file2.jpg', 'http://google.com/file3.jpg'] # etc. > > with AsyncIOExecutor() as asyncio_executor: > files = asyncio_executor.map(download_file, urls) > > And that's it, no coroutines, no `yield from`. Since, if I understand > correctly, asyncio requires a mainloop, it would make sense for the > AsyncIOExecutor to have a thread of its own in which it could run its > mainloop. > > Is this possible? Did someone implement this? > Do you really need to upload/download that many at the same time? I'd put them into a queue and have only a few of uploading/downloading at any time.