Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'cache': 0.05; 'url:launchpad': 0.05; 'postgresql': 0.07; 'problem:': 0.07; 'suppose': 0.07; 'python': 0.09; 'blocking': 0.09; 'handler,': 0.09; 'hiding': 0.09; 'imply': 0.09; 'objects.': 0.09; 'stored': 0.10; 'def': 0.10; 'extension': 0.13; 'resulting': 0.13; '(python)': 0.16; '1sec': 0.16; 'async': 0.16; "db's": 0.16; 'forking': 0.16; 'query.': 0.16; 'skip:p 70': 0.16; 'threads': 0.16; 'twisted': 0.16; 'wrote:': 0.17; 'thanks,': 0.18; 'memory': 0.18; 'requests': 0.18; 'not,': 0.21; 'amounts': 0.22; 'thus': 0.24; 'connected': 0.24; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'looks': 0.26; '(as': 0.27; 'this?': 0.28; 'run': 0.28; 'email name:': 0.29; 'i/o': 0.29; 'objects': 0.29; 'class': 0.29; 'compatible': 0.30; 'running': 0.32; 'skip:s 30': 0.33; 'like:': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; '(with': 0.33; 'times.': 0.33; 'server': 0.35; 'whatever': 0.35; 'data,': 0.35; 'doing': 0.35; 'pm,': 0.35; 'subject:?': 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'skip:m 40': 0.36; 'client': 0.36; 'too': 0.36; 'possible': 0.37; 'does': 0.37; 'option': 0.37; 'being': 0.37; 'why': 0.37; 'passed': 0.37; 'subject:: ': 0.38; 'behind': 0.38; 'store': 0.38; 'object': 0.38; 'skip:o 20': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'short': 0.39; 'end': 0.40; 'most': 0.61; 'high': 0.61; 'received:network': 0.61; 'more.': 0.62; 'time,': 0.62; 'is.': 0.62; 'relatively': 0.62; 'more': 0.63; 'decided': 0.65; 'limit': 0.65; 'state,': 0.65; 'theoretical': 0.65; 'header:Reply-To:1': 0.68; 'received:ch': 0.69; 'biggest': 0.71; 'reply-to:no real name:2**0': 0.72; '100': 0.78; 'low': 0.83; '(web': 0.84; 'nagy': 0.84; '(running': 0.91; 'capability': 0.91; 'faster.': 0.91 Date: Sat, 01 Sep 2012 07:18:03 -1000 From: Werner Thie Organization: Thie & Co Projects User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:15.0) Gecko/20120824 Thunderbird/15.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Async client for PostgreSQL? References: <50419A55.3080904@shopzeus.com> In-Reply-To: <50419A55.3080904@shopzeus.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: werner@thieprojects.ch 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: 67 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1346520288 news.xs4all.nl 6877 [2001:888:2000:d::a6]:35106 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:28217 On 8/31/12 7:17 PM, Laszlo Nagy wrote: > Is there any extension for Python that can do async I/O for PostgreSQL > with tornadoweb's ioloop? > > Something like: > > class MainHandler(tornado.web.RequestHandler): > @tornado.web.asynchronous > def get(self): > pg_connection.(long_taking_query_sql,params,callback=self.on_query_opened) > > def on_query_opened(self, query): > self.write(process_rows(query)) > self.finish() > > > > What would be an alternative? > > The theoretical problem: suppose there are 100 clients (web browsers) > connected to the server with keep alive connections. They are doing > long-polls and they are also sending/receiving events (with short > response times). Each web browser has an associated state stored on the > server side, in the memory (as an object tree). The state is bound to > the client with a session id. Most requests will have to be responded > with small amounts of data, calculated from the session state, or > queried from the database. Most database queries are simple, running for > about 100msec. But a few of them will run for 1sec or more. Number of > requests ending in database queries is relatively low (10/sec). Other > requests can be responded must faster. but they are much more frequent > (100/sec, that is. 1 request/sec/client). There is a big global cache > full of (Python) objects. Their purpose is to reduce the number of > database queries. These objects in the global cache are emitting events > to other objects found in the client sessions. Generally, it is not > possible to tell what request will end in a database query. > > Multi-threading is not an option because number of clients is too high > (running 100 threads is not good). This is why I decided to use anyc > I/O. Tornadoweb looks good for most requirements: async i/o, store > session state in objects etc. The biggest problem is that psycopg is not > compatible with this model. If I use blocking I/O calls inside a request > handler, then they will block all other requests most of the time, > resulting in slow response times. > > What would be a good solution for this? > > Thanks, > > Laszlo > 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(). Brute force I would pack the db access into a twisted run web-service, forking work out in twisted either with deferToThread() or if you want to take advantage of using processes instead of threads thus being able to tap all cores, then have a look at ampoule at https://launchpad.net/ampoule - be aware though that ampoule has a 64k limit on what can be passed around. Werner