23

I'm interested in running Django on an async framework like Concurrence or gevent. Both frameworks come with its own async MySQL driver.

Problem is Django only officially supports MySQLdb. What do I need to do to make Django work with the MySQL drivers that come with gevent or Concurrence?

Is there a step-by-step guide somewhere that I can follow? Is this a major undertaking?

Thanks.

Continuation
  • 12,722
  • 20
  • 82
  • 106
  • Assuming the API of gevent-mysql matches MySQLdb a very simple custom database backend would be trivial to write and use. I know others would be interested so please share if/when you make this. People in #gevent on freenode would help with details I bet. – tmc Jun 30 '11 at 10:10
  • https://github.com/petehunt/PyMySQL is a pure-python MySQLdb api compatible client library, Mozilla is using it with gevent for the Firefox Sync server. You could either write a small custom db engine (as I reccomend above) or simply use this method: https://github.com/petehunt/PyMySQL/blob/master/pymysql/__init__.py#L110 to patch PyMySQL in. – tmc Aug 23 '11 at 19:35

2 Answers2

37

three cheers for @traviscline's suggestion to go with pymysql. his suggestion was based on this post from mozilla. all it takes is a simple patch to your manage.py file

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

changing the import in your settings file, and monkeypatch() since pymysql is a pure python driver.

travis mentioned that he tests for compatability by changing the imports and running the unittests for pymysql, mysqldb, and myconnpy.

note that there are already examples of finer details to watch out for - but overall this is an elegant, maintainable solution. i will update when i get this running in production!

dnozay
  • 23,846
  • 6
  • 82
  • 104
egbutter
  • 810
  • 11
  • 21
  • 3
    I get a `Thing2Literal` error (doesn't look like that's implemented in pymysql?) – Justin Feb 28 '13 at 18:52
  • this is not a problem if you are using a recent enough release of mysqldb. http://stackoverflow.com/a/15315546/424380 – egbutter Mar 03 '14 at 02:46
  • also, bug 668664 mentioned above (killing queries when the parent process gets killed) was never a noticeable problem for me in production .. the site did not see more than a few thousand requests per day, though, while i was working on it. – egbutter Mar 03 '14 at 02:53
  • This just worked after a lot of effort trying to get the default mysql drivers to work in Python 3.4 with Django. Thanks! – Ronnie Nov 11 '14 at 17:57
  • I am on OSX 10.11, using Mamp, and this solution works for the Python 3.6 and Django – Telvin Nguyen Jun 03 '16 at 07:43
-1

I was successful in getting pymysql to work with Django doing the following :

  1. Comment out the try-except block at the beginning of the base.py file, where MySQLdb is imported.
  2. Add the following four lines to base.py

    try:
        import pymysql as Database
    except ImportError:
        pass
    
  3. As mentioned in the link that egbutter posted, go to the base.py file and find-replace MySQLdb with pymysql at relevant portions of the file, i.e. don't bother changing the error messages (you could, but that's up to you).

  4. Save base.py, and run the following command from the apt location to see the server start up.

    python manage.py runserver
    
Community
  • 1
  • 1