Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Asynchronous PostgreSQL
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 44 - Collapse all  -  Translate all to Translated (View all originals)   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Frank Smit  
View profile  
 More options Mar 1 2011, 4:46 pm
From: Frank Smit <fr...@61924.nl>
Date: Tue, 1 Mar 2011 13:46:23 -0800 (PST)
Local: Tues, Mar 1 2011 4:46 pm
Subject: Asynchronous PostgreSQL
Hello,

I'm planning to use Tornado for a project I'm working on. And I've
decided to use PostgreSQL to store all the data. I read that Psycopg
has asynchronous support[1] and I've been wondering if it's possible
to integrate this with Tornado. I'm not sure how to do this any help
will be appreciated.

Regards,
Frank

  [1]: http://initd.org/psycopg/docs/advanced.html#asynchronous-support


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Cliff Wells  
View profile  
 More options Mar 1 2011, 5:22 pm
From: Cliff Wells <cl...@develix.com>
Date: Tue, 01 Mar 2011 14:22:52 -0800
Local: Tues, Mar 1 2011 5:22 pm
Subject: Re: Asynchronous PostgreSQL
I haven't tried it, but there's an async Twisted driver here:

https://launchpad.net/txpostgres

It uses the async features of psycopg2, so it would be a good starting
point for a Tornado driver.

Another option (that I have used) is ngx_postgres.  It works well, but
requires making your queries via HTTP which brings its own slew of
excitement:

https://github.com/FRiCKLE/ngx_postgres/

Regards,
Cliff


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Darnell  
View profile  
 More options Mar 1 2011, 5:24 pm
From: Ben Darnell <b...@bendarnell.com>
Date: Tue, 1 Mar 2011 14:24:31 -0800
Local: Tues, Mar 1 2011 5:24 pm
Subject: Re: Asynchronous PostgreSQL

You need to use the IOLoop {add,update,remove}_handler methods to listen for
updates on the connection's fileno().  Very roughly:

class AsyncConnection:
  def __init__(self, conn):
    self.conn = conn
    self.callback = None

  def query(self, args, callback):
    self.conn.execute(args)
    self.callback = callback
    self._update_handler()

  def _update_handler(self):
    state = self.conn.poll()
    if state == psycopg2.extensions.POLL_OK:
      callback = self.callback
      self.callback = None
      callback()
    elif state == psycopg2.extensions.POLL_READ:
      IOLoop.instance().add_handler(self.conn.fileno(), IOLoop.READ,
        self._io_callback)
    elif state == psycopg2.extensions.POLL_WRITE:
      IOLoop.instance().add_handler(self.conn.fileno(), IOLoop.WRITE,
        self._io_callback)

  def _io_callback(self, events):
    # maybe keep track of the previous state so you can use update_handler
    # instead of remove/add
    IOLoop.instance().remove_handler(self.conn.fileno())
    self._update_handler()

-Ben


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Frank Smit  
View profile  
 More options Mar 2 2011, 3:44 pm
From: Frank Smit <fr...@61924.nl>
Date: Wed, 2 Mar 2011 21:44:30 +0100
Local: Wed, Mar 2 2011 3:44 pm
Subject: Re: Asynchronous PostgreSQL
Ok, I tried the code: http://pastebin.com/1XAcfji9 - And this is the
output: http://pastebin.com/mAHn0D03

I'm not really familiar with this.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Frank Smit  
View profile  
 More options Mar 2 2011, 5:24 pm
From: Frank Smit <fr...@61924.nl>
Date: Wed, 2 Mar 2011 23:24:29 +0100
Local: Wed, Mar 2 2011 5:24 pm
Subject: Re: Asynchronous PostgreSQL
I've changed some things in the code now and it works:
http://pastebin.com/6DyxHBrV. I shouldn't ask too soon for help when
it doesn't work. :)

Thanks for the help!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Bengtsson  
View profile  
 More options Mar 3 2011, 10:05 am
From: Peter Bengtsson <pete...@gmail.com>
Date: Thu, 3 Mar 2011 07:05:37 -0800 (PST)
Local: Thurs, Mar 3 2011 10:05 am
Subject: Re: Asynchronous PostgreSQL

I wish I could put a big fat star on that piece of code.

Man, we really need a site to collect all these nifty snippets related to
Tornado.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Frank Smit  
View profile  
 More options Mar 3 2011, 10:53 am
From: Frank Smit <fr...@61924.nl>
Date: Thu, 3 Mar 2011 16:53:58 +0100
Local: Thurs, Mar 3 2011 10:53 am
Subject: Re: Asynchronous PostgreSQL
It's not in a very usable state right now. Just after I mailed my
message I discovered that the cursor can not do two concurrent queries
(I read about it). You can see it when you start the script and go to
your browser at localhost:whateverport and refresh a couple of times
(very quickly). You'll see in the terminal that it throws an
exception.

I'm going to take a look at it this evening.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeremy Kelley  
View profile  
 More options Mar 3 2011, 1:33 pm
From: Jeremy Kelley <jer...@33ad.org>
Date: Thu, 3 Mar 2011 12:33:36 -0600
Local: Thurs, Mar 3 2011 1:33 pm
Subject: Re: Asynchronous PostgreSQL

On Thu, Mar 3, 2011 at 9:05 AM, Peter Bengtsson <pete...@gmail.com> wrote:
> I wish I could put a big fat star on that piece of code.
> Man, we really need a site to collect all these nifty snippets related to
> Tornado.

Just create a gist on gist.github.com  or create a public repository
(github, bitbucket, etc).  Then, link to them from the tornado wiki.

Not ultra-sexy but very low cost of entry and available today.

-j

--
The Christian ideal has not been tried and found wanting;
it has been found difficult and left untried – G. K. Chesterton


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
emcconne  
View profile  
 More options Mar 4 2011, 10:58 am
From: emcconne <emcco...@gmail.com>
Date: Fri, 4 Mar 2011 07:58:02 -0800 (PST)
Local: Fri, Mar 4 2011 10:58 am
Subject: Re: Asynchronous PostgreSQL
You mentioned FRiCKLE/ngx_postgres as a async option for Postgres, but
then went on to say "via HTTP which brings its own slew of
excitement".  Care to elaborate on any of your experiences for those
of us new to ngx_postgres?

Regards,
Brent

On Mar 1, 5:22 pm, Cliff Wells <cl...@develix.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Carl S. Yestrau Jr.  
View profile  
 More options Mar 4 2011, 11:32 am
From: "Carl S. Yestrau Jr." <c...@featureblend.com>
Date: Fri, 4 Mar 2011 08:32:26 -0800
Local: Fri, Mar 4 2011 11:32 am
Subject: Re: Asynchronous PostgreSQL
I'm currently using it. You'll have to compile your own nginx with the
custom modules. What really bit me was the escape routines (everything
seems to be escaped as a string) and complex conditional sql that you
would normally generate with logic. It's pretty cool once you have it
up and running but the ramp up might kill features you could have
otherwise built.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Cliff Wells  
View profile  
 More options Mar 4 2011, 12:10 pm
From: Cliff Wells <cl...@develix.com>
Date: Fri, 04 Mar 2011 09:10:46 -0800
Local: Fri, Mar 4 2011 12:10 pm
Subject: Re: Asynchronous PostgreSQL

On Fri, 2011-03-04 at 07:58 -0800, emcconne wrote:
> You mentioned FRiCKLE/ngx_postgres as a async option for Postgres, but
> then went on to say "via HTTP which brings its own slew of
> excitement".  Care to elaborate on any of your experiences for those
> of us new to ngx_postgres?

I was making reference to trying to map relational datasets onto a
RESTful interface.  If you're comfortable doing that (I've found using
lots of server-side views to be key) then it's mostly just a discovery
process.   I know for me it took lots and lots of refactoring, but
overall I was fairly happy with the result.

Regards,
Cliff


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Frank Smit  
View profile  
 More options Mar 4 2011, 3:41 pm
From: Frank Smit <fr...@61924.nl>
Date: Fri, 4 Mar 2011 21:41:13 +0100
Local: Fri, Mar 4 2011 3:41 pm
Subject: Re: Asynchronous PostgreSQL
Hi, I've made a simple connection pool (not completely finished) and
it works without problems (except one). The only problem is that the
Tornado server can't fork into multiple processes, because the IOloop
is initiated before the server is started. That's what the exception
tells me. Besides that I think everything works.

Here's the code: http://pastebin.com/JAng2yRU. Would be cool if people
can test it. :) You only need to start a PostgreSQL database and
change the settings in the script.

Regards,
Frank


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Frank Smit  
View profile  
 More options Mar 8 2011, 5:14 pm
From: Frank Smit <fr...@61924.nl>
Date: Tue, 8 Mar 2011 23:14:25 +0100
Local: Tues, Mar 8 2011 5:14 pm
Subject: Re: Asynchronous PostgreSQL
Here's my last version: https://gist.github.com/861193

An example is included. I think everything works now. Feedback is
welcome. Have fun.

Regards,
Frank


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
paolo.losi  
View profile   Translate to Translated (View Original)
 More options Mar 11 2011, 8:40 am
From: "paolo.losi" <paolo.l...@gmail.com>
Date: Fri, 11 Mar 2011 05:40:07 -0800 (PST)
Local: Fri, Mar 11 2011 8:40 am
Subject: Re: Asynchronous PostgreSQL
Frank,

nice and interesting work!
Would you mind creating a project on github?
I'd be more than willing to contribute to it...

Paolo

On Mar 8, 11:14 pm, Frank Smit <fr...@61924.nl> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Frank Smit  
View profile  
 More options Mar 11 2011, 12:53 pm
From: Frank Smit <fr...@61924.nl>
Date: Fri, 11 Mar 2011 18:53:27 +0100
Local: Fri, Mar 11 2011 12:53 pm
Subject: Re: Asynchronous PostgreSQL
Here you go: https://github.com/FSX/momoko


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Frank Smit  
View profile  
 More options Mar 26 2011, 5:48 pm
From: Frank Smit <fr...@61924.nl>
Date: Sat, 26 Mar 2011 22:48:28 +0100
Local: Sat, Mar 26 2011 5:48 pm
Subject: Re: Asynchronous PostgreSQL
I got some time again and want to work on unit tests. I haven't used
this too much and was wondering how I should make unit tests for this
wrapper.

How do you handle unit tests with a database that needs to be
configured? And what should I exactly test?

Regards,
Frank


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Bengtsson  
View profile  
 More options Mar 28 2011, 6:12 am
From: Peter Bengtsson <pete...@gmail.com>
Date: Mon, 28 Mar 2011 03:12:18 -0700 (PDT)
Local: Mon, Mar 28 2011 6:12 am
Subject: Re: Asynchronous PostgreSQL

I actually tried it and it worked really well. Thanks. However my scenario
was very basic and just an excuse to play with your code.

However, I quickly gave up when I realised I had to actually write the SQL
:)

One thing that would have helped, would be if I could mix synchronous with
asynchronous. Especially since a couple of queries leading up to a big slow
one doesn't need to do a bunch async callbacks.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Frank Smit  
View profile  
 More options Mar 28 2011, 7:02 am
From: Frank Smit <fr...@61924.nl>
Date: Mon, 28 Mar 2011 13:02:41 +0200
Local: Mon, Mar 28 2011 7:02 am
Subject: Re: Asynchronous PostgreSQL
I haven't actually done anything advanced with it. Just confirmed that
it worked with one or two query,

It's only a wrapper for Psycopg so it's obvious that you have to write
SQL. ;) You want some kind of ORM?

I was thinking of adding some kind of query chain
[https://github.com/FSX/momoko/issues#issue/3], but I haven't had time
for this yet. The idea is that you only need one callback that is
called once the last query has finished. And maybe optional callbacks
in between. Not sure about his yet.

Regards,
Frank Smit


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Cliff Wells  
View profile  
 More options Mar 28 2011, 2:03 pm
From: Cliff Wells <cl...@develix.com>
Date: Mon, 28 Mar 2011 11:03:18 -0700
Local: Mon, Mar 28 2011 2:03 pm
Subject: Re: Asynchronous PostgreSQL

On Mon, 2011-03-28 at 13:02 +0200, Frank Smit wrote:
> I haven't actually done anything advanced with it. Just confirmed that
> it worked with one or two query,

> It's only a wrapper for Psycopg so it's obvious that you have to write
> SQL. ;) You want some kind of ORM?

Just my two cents: having used SQLobject and SQLAlchemy, I'd take the
plain SQL API any day.  I know a lot of people like ORMs, but I prefer
the simplicity of plain SQL.  ORMs do make some tedious things simple,
but the added mental cost of the abstraction isn't worth it.

> I was thinking of adding some kind of query chain
> [https://github.com/FSX/momoko/issues#issue/3], but I haven't had time
> for this yet. The idea is that you only need one callback that is
> called once the last query has finished. And maybe optional callbacks
> in between. Not sure about his yet.

I wrote a little bit of code that simply acts as an accumulator for
multiple queries.  It's not a chain like described in the link above,
but it does unroll the async callback sequence a bit, which makes things
nicer if you have to do multiple queries for a single page.

class Aggregator (object):
    '''used as a callback that accumulates results of multiple
    separate callbacks and finishes when they are all accounted for
    '''
    def __init__ (self, finish, required):
        '''finish is a callback function to be invoked when all required callbacks are done
        required is a list of names of callbacks (strings)
        '''
        self.finish = finish
        self.required = required
        self.values = { }

    def __call__ (self, which, values):
        self.values [which] = values
        if set (self.required) == set (self.values.keys ()):
            self.finish (self.values)

    def __add__ (self, required):
        self.required.append (required)
        return self

The above class gets used like so (seriously simplified from working
code, so it's certainly broken).

class RequestHandler (BaseHandler):
    @tornado.web.asynchronous
    def get (self, ...):
        def render (values):
            # values is a dictionary, where the keys are the same passed to Aggregator.__init__
            # and the values are the query results.
            # ... do some stuff to values, render a template, etc
            self.write (...)
            self.finish ()

        views = {
            'query1': 'select * from foo',
            'query2': 'select * from bar'  
        }

        ag = Aggregator (render, views.keys ())

        for key, query in views.items ():
            # theoretical call to some async db api
            db.execute (query, callback=functools.partial (ag, key))

Hope that makes sense and I didn't screw it up too badly in the
simplification.  Basically it allows me to have a single callback (an
Aggregator object) that only calls render() once all the queries have
completed.

Regards,
Cliff


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Frank Smit  
View profile  
 More options Mar 29 2011, 5:54 pm
From: Frank Smit <fr...@61924.nl>
Date: Tue, 29 Mar 2011 23:54:59 +0200
Local: Tues, Mar 29 2011 5:54 pm
Subject: Re: Asynchronous PostgreSQL
Hi,

I added a simple implementation of your aggregator, Cliff. I called it
BatchQuery, since I don't know what aggregator means. :) Here's the
commit: https://github.com/FSX/momoko/commit/43fa5fe095c99210ecf4bce6da4d0e84...

The pool cleaner is also fixed. When more than one connections needed
to be removed a IndexError popped up.

Goodnight,
Frank


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Cliff Wells  
View profile  
 More options Mar 29 2011, 9:52 pm
From: Cliff Wells <cl...@develix.com>
Date: Tue, 29 Mar 2011 18:52:34 -0700
Local: Tues, Mar 29 2011 9:52 pm
Subject: Re: Asynchronous PostgreSQL

On Tue, 2011-03-29 at 23:54 +0200, Frank Smit wrote:
> Hi,

> I added a simple implementation of your aggregator, Cliff. I called it
> BatchQuery, since I don't know what aggregator means. :) Here's the
> commit: https://github.com/FSX/momoko/commit/43fa5fe095c99210ecf4bce6da4d0e84...

One issue I see is that it doesn't appear possible to know which queries
you are seeing the results of by the time you reach the callback.  For
instance, if the queries were for a blog, and one query represented
"article by id", and another were for "comments by post", you'd want to
have them named in some way so as to be able to easily reference them
later (this is one reason I used a dict rather than a sequence).

Regards,
Cliff


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Frank Smit  
View profile  
 More options Mar 30 2011, 8:30 am
From: Frank Smit <fr...@61924.nl>
Date: Wed, 30 Mar 2011 14:30:37 +0200
Local: Wed, Mar 30 2011 8:30 am
Subject: Re: Asynchronous PostgreSQL
You're right, didn't think about at the time of writing. Will fix this
when I get home.

Regards,
Frank


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Frank Smit  
View profile  
 More options Mar 31 2011, 5:07 pm
From: Frank Smit <fr...@61924.nl>
Date: Thu, 31 Mar 2011 23:07:05 +0200
Local: Thurs, Mar 31 2011 5:07 pm
Subject: Re: Asynchronous PostgreSQL
Ok, done: https://github.com/FSX/momoko/commit/6b8b62b4e9d3b35eacdc6fb51038e0f6...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrew Zeneski  
View profile  
 More options Mar 31 2011, 7:30 pm
From: Andrew Zeneski <and...@andrewzeneski.com>
Date: Thu, 31 Mar 2011 19:30:01 -0400
Local: Thurs, Mar 31 2011 7:30 pm
Subject: Re: Asynchronous PostgreSQL
Frank,

This BatchQuery implementation looks really cool! Often when I have a series of statements to run, I will want to make sure they run in a specific order. Since dictionaries are unordered, maybe the list idea was better. The return could still be a dict, with the key being the sequence the query was in the original list; or just use the dict in the collect method, then sort it and return a tuple of results back.

Just a thought.

Andrew

On 2011-03-31 17:07:05 -0400, python-tornado@googlegroups.com Wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Cliff Wells  
View profile  
 More options Mar 31 2011, 8:54 pm
From: Cliff Wells <cl...@develix.com>
Date: Thu, 31 Mar 2011 17:54:33 -0700
Local: Thurs, Mar 31 2011 8:54 pm
Subject: Re: Asynchronous PostgreSQL

On Thu, 2011-03-31 at 19:30 -0400, Andrew Zeneski wrote:
> Frank,

> This BatchQuery implementation looks really cool! Often when I have a
> series of statements to run, I will want to make sure they run in a
> specific order. Since dictionaries are unordered, maybe the list idea
> was better. The return could still be a dict, with the key being the
> sequence the query was in the original list; or just use the dict in
> the collect method, then sort it and return a tuple of results back.

If you need them run in a specific order, then async is not the way to
go.  Rather you should simply use the standard sync API or chain your
queries via callbacks.

Regards,
Cliff


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Messages 1 - 25 of 44   Newer >
« Back to Discussions « Newer topic     Older topic »