473,772 Members | 2,244 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(c ursor, buffer=100):
res = cursor.fetchman y(buffer)
while res:
for record in res:
yield record
res = cursor.fetchman y(buffer)

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

result.append(o utput)
print "done"
except:
# just for testing
result.append(N one)
raise

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

result = []

thread.start_ne w_thread(worker , (cursor, sql, result))
thread.start_ne w_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 10577
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.netc om.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 "threadsafe ty" global variable.

HTH,

AdSR
Jul 18 '05 #5
AdSR wrote:

See PEP 249, read about the "threadsafe ty" 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.netc om.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
5088
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, but is there any way to get the number of rows matched? I want to find out, when rows updated = 0, if there were no updates because the row wasn't found (rows matched will = 0) or because the update would not have changed any data (rows matched =...
2
5054
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, sometimes not. From mysql: mysql> show databases; +-----------+ | Database |
21
5260
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 works as expected. But >>> numb=10 >>> cursor.execute("SELECT * FROM mytest where clientID = %d",numb) Traceback (innermost last): File "<stdin>", line 1, in ?
0
1476
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 someone please explain what I'm doing wrong? The table is served off of MySQL 4.0.20 and contains the following fields:
2
2197
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 ------------------------------- #!/usr/bin/python import MySQLdb db = MySQLdb.connect(host="localhost", user="root", passwd="mypass", db="my_db") cursor = db.cursor()
2
4001
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 subject shows, to use a insert statement on the form: statement = INSERT INTO table (colA,colB,colC) values (%s,%s,%s) and pass in a list containing tuples list =
3
2505
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 MySQLdb library for connectivity. I can read from the database no problem, but when I do an insert, the value never gets added to the database, even though there is no error, and the SQL is fine (I print out the SQL statement in the function). When I...
11
25630
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
7506
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 someone point me in the right direction please ? The python looks like this : import MySQLdb import MySQLdb.cursors conn = MySQLdb.Connect(host='localhost', user='abc,passwd='def',
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10104
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8934
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.