472,951 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,951 software developers and data experts.

MySQLDB multiple cursor question

I am trying to use threads and mysqldb to retrieve data from multiple
asynchronous queries.

My basic strategy is as follows, create two cursors, attach them to the
appropriate databases and then spawn worker functions to execute sql
queries and process the results.

This works occasionally, but fails a lot taking python down with it.
Sometimes it also loses connection to the database. Sometimes I get an
error, "Commands out of sync; You can't run this command now" which
makes me suspicious. Of course, I could be doing things completely
wrong. If I can't have multiple cursors by the way, that's just fine
with me. I just thought that I could ;)

I only have one thread or no threads at all it works just fine. I have
tried using thread safe Queues to bundle results and also lists with the
same results.

Can anyone notice anything in the toy code I have attached that would
cause this effect? Thanks for any input.

import MySQLdb, thread, time

def cursoriterate(cursor, buffer=100):
res = cursor.fetchmany(buffer)
while res:
for record in res:
yield record
res = cursor.fetchmany(buffer)

def worker(cursor, sql, result):
try:
print "executing", sql
cursor.execute(sql)
output = []
for record in cursoriterate(cursor):
output.append(cursor)

result.append(output)
print "done"
except:
# just for testing
result.append(None)
raise

for i in range(100):
sql = "select target, result, evalue from BLAST_RESULT where evalue
< 0.001"
db = MySQLdb.connect(user="mergedgraph", host="localhost")
cursor = db.cursor()
cursor.execute("USE HPYLORI_YEAST")
cursor2 = db.cursor()
cursor2.execute("USE HPYLORI_YEAST")

result = []

thread.start_new_thread(worker, (cursor, sql, result))
thread.start_new_thread(worker, (cursor2, sql, result))

while len(result)< 2:
time.sleep(1)

print "results are full"
res = result.pop()
res2 = result.pop()

if res: print len(res)
if res2: print len(res2)
cursor.close()
cursor2.close()
db.close()

Jul 18 '05 #1
7 10315
Brian Kelley wrote:
I am trying to use threads and mysqldb to retrieve data from multiple
asynchronous queries.

My basic strategy is as follows, create two cursors, attach them to the
appropriate databases and then spawn worker functions to execute sql
queries and process the results.


The problem goes away if I have only one cursor per connection and just
use multiple connections. This seems like a bug but I don't know for sure.

Brian

Jul 18 '05 #2
Brian Kelley fed this fish to the penguins on Thursday 08 January 2004
07:58 am:

The problem goes away if I have only one cursor per connection and
just
use multiple connections. This seems like a bug but I don't know for
sure.
f The DB-API specifies a common method for accessing data -- this means
"cursors".

MySQL itself does not implement that type of cursor.

Therefore, MySQLdb has to emulate cursors locally. That emulation may
be tied to one per connection (or, at least, one active per connection
-- maybe doing a conn.commit()?) [This is all hypothesis at this time]

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Bestiaria Home Page: http://www.beastie.dm.net/ <
Home Page: http://www.dm.net/~wulfraed/ <


Jul 18 '05 #3
Dennis Lee Bieber wrote:
f The DB-API specifies a common method for accessing data -- this means
"cursors".

MySQL itself does not implement that type of cursor.

Therefore, MySQLdb has to emulate cursors locally. That emulation may
be tied to one per connection (or, at least, one active per connection
-- maybe doing a conn.commit()?) [This is all hypothesis at this time]


Guess I'll have to crack open the mysqldb source code and fire up a
debugger. The main problem with using multiple connections is that I
have to cache the user's password in order to repoen the connection
which makes me feel very queasy.

The error is very reproducible but that fact that it works sometimes and
not others means that it is probably a bug in mysqldb.

Brian

Jul 18 '05 #4
Brian Kelley <bk*****@wi.mit.edu> wrote:
Brian Kelley wrote:
I am trying to use threads and mysqldb to retrieve data from multiple
asynchronous queries.

My basic strategy is as follows, create two cursors, attach them to the
appropriate databases and then spawn worker functions to execute sql
queries and process the results.


The problem goes away if I have only one cursor per connection and just
use multiple connections. This seems like a bug but I don't know for sure.

Brian


See PEP 249, read about the "threadsafety" global variable.

HTH,

AdSR
Jul 18 '05 #5
AdSR wrote:

See PEP 249, read about the "threadsafety" global variable.
There you have it. MySQLdb has a threadsafety level of 1 which means
that connections can't be shared but the module can.

I guess I'm doing it the right way now :)

HTH,

AdSR


Jul 18 '05 #6
Brian Kelley fed this fish to the penguins on Thursday 08 January 2004
16:28 pm:
There you have it. MySQLdb has a threadsafety level of 1 which means
that connections can't be shared but the module can.
I'd run into a reference to that attribute in the Nutshell, but the
section on DB-API only mentioned that 0 meant not-thread-safe; no
explanation of what different positive values might mean (and I didn't
have time this morning to try to find it via google).
-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Bestiaria Home Page: http://www.beastie.dm.net/ <
Home Page: http://www.dm.net/~wulfraed/ <


Jul 18 '05 #7
Dennis Lee Bieber wrote:
Brian Kelley fed this fish to the penguins on Thursday 08 January 2004
16:28 pm:

There you have it. MySQLdb has a threadsafety level of 1 which means
that connections can't be shared but the module can.


I'd run into a reference to that attribute in the Nutshell, but the
section on DB-API only mentioned that 0 meant not-thread-safe; no
explanation of what different positive values might mean (and I didn't
have time this morning to try to find it via google).


If you google for PEP 249 you'll find the description.

Brian

Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Chris Stromberger | last post by:
When issuing updates in mysql (in the console window), mysql will tell you if any rows matched and how many rows were updated (see below). I know how to get number of rows udpated using MySQLdb,...
2
by: Tim Williams | last post by:
I'm trying to write a simple python program to access a MySQL database. I'm having a problem with using MySQLdb to get the results of a SQL command in a cursor. Sometimes the cursor.execute works,...
21
by: John Fabiani | last post by:
Hi, I'm a newbie and I'm attempting to learn howto create a select statement. When I use >>> string1='18 Tadlock Place' >>> cursor.execute("SELECT * FROM mytest where address = %s",string1) All...
0
by: Wesley Kincaid | last post by:
I'm attempting to run a simple query through MySQLdb's cursor.execute(). However, when the request includes a timestamp field, I'm getting "ValueError: invalid literal for int(): 9-." Could...
2
by: ws Wang | last post by:
MySQLdb is working fine at command line, however when I tried to use it with mod_python, it give me a "server not initialized" error. This is working fine: ----------------------- testmy.py...
2
by: olekristianvillabo | last post by:
The method cursor.executemany is there in order to avoid multiple calls to cursor.execute(). I have tried, with success, to do like every single example (that I have found on the www) on the...
3
by: David Mitchell | last post by:
Hello, I am a complete beginner with Python. I've managed to get mod_python up and running with Apache2 and I'm trying to a simple insert into a table in a MySQL database. I'm using the...
11
by: Fred | last post by:
I hope someone can help me with the below problem... Thanks, Fred My enviroment: -------------------------- Slackware Linux 10.2 Python 2.4.2 MySql version 4.1.14
1
by: shearichard | last post by:
Hi - I have written some python to insert a row into a table using MySQLDB. I have never before written SQL/Python using embedded parameters in the SQL and I'm having some difficulties. Could...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.