MySQL Forums
Forum List  »  Connector/Python

thread safe MySQLdb cursors?
Posted by: Steve Strassmann
Date: July 24, 2006 01:05PM

I've been using MySQLdb in Python for a while with no problems.
I'm using it in an SCGI web server (Apache forwards all requests
to a standalone process running Python/SCGI)

But recently, I've started getting SQL errors, it seems
like my Ajax callbacks are stepping on each other in some
clear violation of thread safety.
Here is a greatly simplified description:

callback1.py is invoked via SCGI
it calls GetMyColorFromSQL()
this creates a cursor with MySQLdb.connect() and
reads a string from the database.


callback2.py is invoked via SCGI
it calls GetMyLengthFromSQL()
this creates a cursor with MySQLdb.connect() and
reads an integer from the database.


What happens is that GetMyLengthFromSQL() calls
cursor.execute(sql, params)
cursor.fetchall()
which returns the strings which are clearly from the other call.


I'm sure the solution lies in some careful management of the
MySQLdb cursors, but I can't find any documentation explaining
what to do. I am careful to create exactly one cursor in each
callback and re-use it throughout the callback.


I tried calling cursor.connection.close() at the end of each callback
and started getting these errors:
Exception _mysql_exceptions.OperationalError: (2006, 'MySQL server
has gone away')

Is there any thing you can think of that I should be doing or
looking at to make these threads more robust?

Options: ReplyQuote


Subject
Written By
Posted
thread safe MySQLdb cursors?
July 24, 2006 01:05PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.