ANN: PyMySQL 0.3

1,262 views
Skip to first unread message

Pete Hunt

unread,
Sep 3, 2010, 2:07:19 PM9/3/10
to Django users
I’m proud to announce the release of PyMySQL 0.3. For those of you
unfamiliar with PyMySQL, it is a pure-Python drop-in replacement for
MySQLdb with an emphasis on compatibility with MySQLdb and for various
Python implementations. I started working on the project due to my
frustrations stemming from getting MySQLdb working on Snow Leopard.
PyMySQL has been tested on CPython 2.3+, Jython, IronPython and PyPy,
and we have an unreleased Python 3.0 branch in Subversion. I encourage
anyone hoping to connect to MySQL from Python to check it out and
report any bugs you might find! Our current focus has been bringing it
up to compatibility with SQLAlchemy and Django, and we have by and
large achieved that goal with a high level of performance.

This is of specific interest to Django users, as one can simply
replace references to "MySQLdb" with "pymysql" and run their Django
apps on platforms that are difficult to get working with MySQLdb (like
Mac OSX Snow Leopard).

Check it out at http://www.pymysql.org/.

Andy

unread,
Sep 3, 2010, 5:20:46 PM9/3/10
to Django users
Pete,

Would you recommend PyMySQL for production use?

Which parts of Django do I need to modify in order to use it with
PyMySQL instead of MySQLdb?

Thanks.

Pete Hunt

unread,
Sep 4, 2010, 4:58:43 PM9/4/10
to Django users
Hi Andy -

Below is a patch that will let Django use pymysql if MySQLdb is not
available. Currently it is not in use in production anywhere but I am
hoping to change that soon. Currently its primary use is prototyping
applications that are being developed on Mac OSX but deployed on
Linux. If you are interested in using it in production please let me
know and I will be sure to prioritize any tickets related to
production issues.

Simply search-replacing MySQLdb with pymysql in django/db/backends/
mysql/base.py and django/db/backends/mysql/introspection.py will get
it to work. I will submit a patch soon that tries to import MySQLdb
but falls back to pymysql if it is not available.

Pete

Andy

unread,
Sep 5, 2010, 12:48:46 AM9/5/10
to Django users
Thanks Pete.

I'm definitely interested in using PyMySQL for production.

I'm interested in running Django in an async mode using gevent (http://
www.gevent.org/). gevent provides a money patch that turns any Python
code into non-blocking. The problem is that MySQLdb is written in C,
so it'd still block, which would kinda defeat the whole purpose of
having an async Django app if every database access is blocking.

PyMySQL looks like could be a great solution for this problem. In fact
there's already a thread in the gevent mailing list talking about
PyMySQL. You might want to drop by:
http://groups.google.com/group/gevent/browse_thread/thread/b5f7bf44b7690ed9

Daniel Roseman

unread,
Sep 5, 2010, 5:29:37 AM9/5/10
to Django users
On Sep 4, 9:58 pm, Pete Hunt <floydoph...@gmail.com> wrote:
> Hi Andy -
>
> Below is a patch that will let Django use pymysql if MySQLdb is not
> available. Currently it is not in use in production anywhere but I am
> hoping to change that soon. Currently its primary use is prototyping
> applications that are being developed on Mac OSX but deployed on
> Linux. If you are interested in using it in production please let me
> know and I will be sure to prioritize any tickets related to
> production issues.
>
> Simply search-replacing MySQLdb with pymysql in django/db/backends/
> mysql/base.py and django/db/backends/mysql/introspection.py will get
> it to work. I will submit a patch soon that tries to import MySQLdb
> but falls back to pymysql if it is not available.
>
> Pete

It occurs to me that a better solution for this might be to create a
custom DB backend for Django - then you could install that in your
Pythonpath and reference it in your settings.py, without having to
patch Django. It shouldn't be too hard to create - just subclass the
MySQLdb backend but import pymsql instead. If I get some time I'll try
and work up an example.
--
DR.

Andy

unread,
Sep 5, 2010, 12:23:40 PM9/5/10
to Django users


On Sep 5, 5:29 am, Daniel Roseman <dan...@roseman.org.uk> wrote:

> If I get some time I'll try and work up an example.

Please do Daniel. That'd really help.

Andy Dustman

unread,
Sep 10, 2010, 11:18:40 AM9/10/10
to django...@googlegroups.com
On Sun, Sep 5, 2010 at 12:48 AM, Andy <selfor...@gmail.com> wrote:
> Thanks Pete.
>
> I'm definitely interested in using PyMySQL for production.
>
> I'm interested in running Django in an async mode using gevent (http://
> www.gevent.org/). gevent provides a money patch that turns any Python
> code into non-blocking. The problem is that MySQLdb is written in C,
> so it'd still block, which would kinda defeat the whole purpose of
> having an async Django app if every database access is blocking.

Uh, no.

MySQLdb releases the GIL on any blocking call, so other threads can run.

Django is *not* asynchronous. It's threaded.
--
Question the answers

Andy

unread,
Sep 10, 2010, 12:25:33 PM9/10/10
to Django users
On Sep 10, 11:18 am, Andy Dustman <farcep...@gmail.com> wrote:

> Uh, no.
>
> MySQLdb releases the GIL on any blocking call, so other threads can run.
>
> Django is *not* asynchronous. It's threaded.

The default Django behavior is threaded.

But it can be made to run in async mode, where socket communication
doesn't block. That way one Django thread can serve multiple users
concurrently.

Check out gevent for details.

Bo Shi

unread,
Sep 29, 2010, 6:56:09 PM9/29/10
to Django users
The following patch to your application's manage.py will allow you to
use pymysql without patching Django.

#!/usr/bin/env python
+try:
+ import pymysql
+ pymysql.install_as_MySQLdb()
+except ImportError:
+ pass
+

Andy

unread,
Sep 30, 2010, 8:05:53 AM9/30/10
to Django users
Great!

Thank you very much.
Reply all
Reply to author
Forward
0 new messages