Re: [tornado] Tornado different databases support

323 views
Skip to first unread message

Frank Smit

unread,
Jun 11, 2012, 12:50:34 PM6/11/12
to python-...@googlegroups.com
You can just use Psycopg2 in Tornado. There's no need for special
support, but it will not be asynchronous.

If you want to use an asynchronous PostgreSQL driver, here:
http://momoko.61924.nl/

Also check the list of other libraries for Tornado:
https://github.com/facebook/tornado/wiki/Links


On Mon, Jun 11, 2012 at 5:23 AM, SvartalF <se...@svartalf.info> wrote:
> For me lack of support of PostgreSQL database in a tornado is a bad thing.
>
> I had found this pull request
> (https://github.com/facebook/tornado/pull/419), which was unaccepted due a
> API design problems, and thought that it's a good idea - make support for
> different databases in the tornado.
>
> Current implementation has support for two databases: for MySQL and
> PostgreSQL:
> https://github.com/svartalf/tornado/tree/db-refactoring/tornado/database
>
> For a easy usage there is a function `tornado.database.connect` which is
> accepts all the arguments like an old-n-good `tornado.database.Connection`,
> except one thing: first parameter is a database type string. It looks like
> this:
>
>
> from tornado import database
>
> mysql = database.connect('mysql', 'localhost', 'test_db', user='root')
> postgresql = database.connect('postgresql_psycopg2', 'localhost', 'pg_db',
> user='tirion')
>
>
> Both `mysql` and `postgresql` variables are now a Connection class
> instances, like in a current tornado implementation.
>
> At now it is still in development, has no tests and documentation, but I
> wanted to discuss about a code style and an API design, so I can know that
> development is moving into a right direction. What do you think about all of
> this?

Alek Storm

unread,
Jun 11, 2012, 9:12:12 PM6/11/12
to python-...@googlegroups.com
I think database support is entirely orthogonal to a web framework, and including it in Tornado in the first place was a mistake. A common non-blocking DB API is a noble goal, but should be relegated to a separate library. IIRC, both `database` and `auth` were on the chopping block for 3.0, but I can't find the email that mentioned them - perhaps it never existed.

Alek

On Mon, Jun 11, 2012 at 7:58 PM, SvartalF <se...@svartalf.info> wrote:
Yes, I know about psycopg2 usage and momoko, thank you.

But I think up at least three reasons of different importance level for this kind of refactoring:

* Unified API to databases. For example, there can be a support of the nosql databases with the same API, something like this:

    conn = tornado.database.connect('mongodb', 'localhost', 'ggroups')
    conn.query('users', {'name': 'Kane'})

MongoDB backend here can be third-side module, which is automatically can be found by a tornado' db backend loader.

* Little hidden optimizations like a server-side cursors in the query result iterators or something else.

* Fallback support for database replacement drivers.
For example, a little refactoring of this code (https://github.com/facebook/tornado/blob/master/tornado/database.py#L223) will allows to use `mysql-ctypes` or `pymysql` instead of a `MySQL-python`.

Fully async db connections will be better of course, but it will cause a very huge refactoring job, it must to be discussed firstly. As for me, I think that adisp-style database queries will be great.

Frank Smit

unread,
Jun 12, 2012, 2:27:20 AM6/12/12
to python-...@googlegroups.com
PEP 249 already encourages a unified API for database drivers:
http://www.python.org/dev/peps/pep-0249/

Ben Darnell

unread,
Jun 14, 2012, 2:27:17 AM6/14/12
to python-...@googlegroups.com
I tend to agree - database.py is there because it was used in one of
the demos and it's just useful enough that it seemed to make sense to
include it, but it doesn't relate to anything else in the framework.
Any efforts to make a better database library should probably happen
outside of tornado, which means that it may be best to yank the module
rather than leave it in its current anemic state. (I still kind of
like it for quick scripts, though, so I'd probably put it up on pypi
under a different name)

As for auth.py, while I wouldn't have added it if it hadn't existed
before I got involved, it's very widely used and I don't want to be
too quick to get rid of it. If it were under active development I
might try to steer it into an independent project, but as things stand
now it's probably not worth rocking the boat and moving it out.

-Ben

youenn boussard

unread,
Jun 14, 2012, 4:22:29 AM6/14/12
to python-...@googlegroups.com
Hello,

I notice that postgres have an async driver which is great.
Can we use mysql in non blocking mode as postgres and witch driver we can use . I notice that concurrence framework have an async driver but can we use it in tornado ? 
If there is example about this , it was great.

Regards Youenn. 
Reply all
Reply to author
Forward
0 new messages