Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!news.stack.nl!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'talks': 0.03; 'cpython': 0.05; 'difference,': 0.09; 'here?': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:skip:c 10': 0.09; 'def': 0.12; 'thread': 0.14; '#####': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'starts': 0.20; 'import': 0.22; 'header:User-Agent:1': 0.23; "i've": 0.25; 'asking': 0.27; 'header:X-Complaints-To:1': 0.27; 'idea': 0.28; 'needed.': 0.30; "i'm": 0.30; 'code': 0.31; 'requests': 0.31; 'writes:': 0.31; 'everyone': 0.33; 'running': 0.33; "i'd": 0.34; 'test': 0.35; "i'll": 0.36; 'should': 0.36; 'message-id:@gmail.com': 0.38; 'to:addr:python-list': 0.38; 'expensive': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'new': 0.61; 'name': 0.63; 'more': 0.64; 'situation': 0.65; 'results': 0.69; 'received:89': 0.85 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Akira Li <4kir4.1i@gmail.com> Subject: Re: Efficiency, threading, and concurrent.futures Date: Wed, 20 Aug 2014 21:17:28 +0400 References: <20140820094335.2345085f@rg.highlandtechnology.com> Mime-Version: 1.0 Content-Type: text/plain X-Gmane-NNTP-Posting-Host: 89.169.229.68 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux) Cancel-Lock: sha1:FDQp0YBnXSulhx9n6dyLTzaHulM= 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: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1408555072 news.xs4all.nl 2878 [2001:888:2000:d::a6]:35346 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:76679 Rob Gaddi writes: > I've got a situation where I'll be asking an I/O bound process to do > some work (querying an RS-232 device) while my main code is off > running a sleep() bound process. Everyone always talks about how > expensive thread creation is, so I figured I'd test it out in an > IPython notebook. > > ##### > > import threading > from concurent.futures import ThreadPoolExecutor as TPE > from time import sleep > > def fn(): > sleep(0.001) > > %%timeit -r 50 -n 1000 > thr = threading.Thread(target=fn) > thr.start() > thr.join() > 1000 loops, best of 5: 1.24 ms per loop > > %%timeit -r 50 -n 1000 ex=TPE(1) > fut=ex.submit(fn) > fut.result() > 1000 loops, best of 5: 1.26 ms per loop > > ##### > > Now, my understanding is that the ThreadPoolExecutor spawns all its > threads at the outset, then stuffs requests into one queue and > fishes results out of another, which should be substantially faster than > having create new threads each time. And yet those were pretty dead on > even. Any idea what I'm seeing here? To see any difference, you should submit more than one job per worker to ThreadPoolExecutor and avoid waiting for the each result synchronously. I don't know whether ThreadPoolExecutor starts all workers at once in the current CPython implementation. The name max_workers suggests that it may start them as needed. -- Akira