Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'url:pypi': 0.03; 'context': 0.05; 'initialize': 0.05; 'apis': 0.07; 'api': 0.09; 'python': 0.09; '"run': 0.09; 'blocked': 0.09; 'blocking': 0.09; 'fetch': 0.09; 'hiding': 0.09; 'imply': 0.09; 'non-blocking': 0.09; 'server:': 0.09; 'to:addr:comp.lang.python': 0.09; 'underlying': 0.09; 'cc:addr:python-list': 0.10; 'stored': 0.10; 'thread': 0.11; 'extension': 0.13; ':-)': 0.13; 'commit': 0.15; '(am': 0.16; '(rather': 0.16; 'async': 0.16; 'btw': 0.16; "db's": 0.16; 'expensive,': 0.16; 'gil,': 0.16; 'hardware.': 0.16; 'interacting': 0.16; 'least.': 0.16; 'query,': 0.16; 'slow,': 0.16; 'thread.': 0.16; 'thread?': 0.16; 'threads': 0.16; 'twisted': 0.16; 'wrote:': 0.17; 'thanks,': 0.18; 'not,': 0.21; '(by': 0.22; '(usually': 0.22; 'finally,': 0.22; 'parse': 0.22; 'example': 0.23; 'external': 0.24; 'cc:2**1': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'scale': 0.27; "doesn't": 0.28; 'run': 0.28; 'i/o': 0.29; 'case,': 0.29; 'probably': 0.29; 'this.': 0.29; "i'm": 0.29; 'query': 0.30; 'function': 0.30; 'seconds': 0.30; 'point': 0.31; 'url:python': 0.32; 'operate': 0.32; 'running': 0.32; 'could': 0.32; 'rid': 0.33; 'safely': 0.33; 'another': 0.33; 'version': 0.34; 'received:google.com': 0.34; 'wrong': 0.34; 'whatever': 0.35; 'open': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'url:org': 0.36; 'cc:no real name:2**1': 0.36; 'expensive': 0.36; 'modules': 0.36; 'useful': 0.36; 'too': 0.36; 'does': 0.37; 'uses': 0.37; 'quite': 0.37; 'received:209': 0.37; 'well.': 0.37; 'subject:: ': 0.38; 'behind': 0.38; 'mean': 0.38; 'think': 0.40; 'from:no real name:2**0': 0.60; 'most': 0.61; 'dedicated': 0.61; 'here:': 0.62; 'invest': 0.62; 'transaction.': 0.62; 'close': 0.63; 'skip:n 10': 0.63; 'more': 0.63; 'messaging': 0.65; 'url:10': 0.65; 'talking': 0.66; 'url:0': 0.67; 'money': 0.74; '100': 0.78; '10%': 0.81; 'maybe,': 0.84; 'more:': 0.84; 'nagy': 0.84; 'pool?': 0.84; 'url:api': 0.84; 'url:documents': 0.84; 'capability': 0.91; 'received:209.85.213.184': 0.91; 'received:mail- yx0-f184.google.com': 0.91; 'insane': 0.93; 'clients,': 0.95; 'url:internet': 0.97 Newsgroups: comp.lang.python Date: Mon, 3 Sep 2012 15:09:26 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=174.252.48.95; posting-account=vglGDQoAAABmaDKgNY6lPt_hwJvC5hhE References: <50419A55.3080904@shopzeus.com> <5042434B.3010201@thieprojects.ch> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 174.252.48.95 MIME-Version: 1.0 Subject: Re: Async client for PostgreSQL? From: calderone.jeanpaul@gmail.com To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 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: , Message-ID: Lines: 117 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1346710169 news.xs4all.nl 6844 [2001:888:2000:d::a6]:48690 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:28374 On Saturday, September 1, 2012 3:28:52 PM UTC-4, Laszlo Nagy wrote: > > Hi > > > > > > does running on tornado imply that you would not consider twisted > > > http://twistedmatrix.com ? > > > > > > If not, twisted has exactly this capability hiding long running > > > queries on whatever db's behind deferToThread(). > > All right, I was reading its documentation > > > > http://twistedmatrix.com/documents/10.1.0/api/twisted.internet.threads.deferToThread.html > > > > It doesn't tell too much about it: "Run a function in a thread and > > return the result as a Deferred.". > You can find more documentation here: http://twistedmatrix.com/documents/current/core/howto/threading.html Also, Twisted has dedicated APIs for interacting with databases asynchronously: http://twistedmatrix.com/documents/current/core/howto/rdbms.html Additionally, there is a non-blocking (rather than thread-based) implementation of the above API available for PostgreSQL: http://pypi.python.org/pypi/txpostgres > > > Run a function but in what thread? Does it create a new thread for every > > invocation? In that case, I don't want to use this. My example case: 10% > > from 100 requests/second deal with a database. But it does not mean that > > one db-related request will do a single db API call only. They will > > almost always do more: start transaction, parse and open query, fetch > > with cursor, close query, open another query etc. then commit > > transaction. 8 API calls to do a quick fetch + update (usually under > > 100msec, but it might be blocked by another transaction for a while...) > > So we are talking about 80 database API calls per seconds at least. It > > would be insane to initialize a new thread for each invocation. And > > wrapping these API calls into a single closure function is not useful > > either, because that function would not be able to safely access the > > state that is stored in the main thread. Unless you protet it with > > locks. But it is whole point of async I/O server: to avoid using slow > > locks, expensive threads and context switching. > > > > Maybe, deferToThread uses a thread pool? But it doesn't say much about > > it. (Am I reading the wrong documentation?) BTW I could try a version > > that uses a thread pool. > > > > It is sad, by the way. We have async I/O servers for Python that can be > > used for large number of clients, but most external modules/extensions > > do not support their I/O loops. Including the extension modules of the > > most popular databases. So yes, you can use Twisted or torandoweb until > > you do not want to call *some* API functions that are blocking. (By > > *some* I mean: much less blocking than non-blocking, but quite a few.) > > We also have synchronous Python servers, but we cannot get rid of the > > GIL, Python threads are expensive and slow, so they cannot be used for a > > large number of clients. And finally, we have messaging services/IPC > > like zeromq. They are probably the most expensive, but they scale very > > well. But you need more money to operate the underlying hardware. I'm > > starting to think that I did not get a quick answer because my use case > > (100 clients) fall into to the "heavy weight" category, and the solution > > is to invest more in the hardware. :-) > > > > Thanks, > > > > Laszlo