[mod_python] Passing Cursor Objects

Dustin Mitchell dustin at ywlcs.org
Thu Jul 17 23:38:45 EST 2003


On Thu, Jul 17, 2003 at 09:00:43PM -0700, Greenbeard wrote:
> Sorry bout the premature email.  Stupid touchpad on
> the laptop here.....
> 
> Is it possible to pass the acutal cursor between
> requests? 
> 
> I can attach the cursor as a value on an <input> tag
> and when I submit the form and I print the variable
> passed I get the <MySQLdb.cursors.Cursor instance at
> 0x04AC1038> but if I reference it in future operations
> I get "'str' object has no attribute 'execute' "
> 
> Based on the documentation I see that I could pass the
> connection or I can move the cursor information out of
> the cursor and pass it as a string.  But am I missing
> something here that would allow me to use the cursor
> object?  
> 
> I am using the publisher handler if that makes a
> difference. 

Nope, you can't do it.  Basically all you can send to the client (the
browser) is text.   Most browsers are nice enough to send your text
back unmangled, but they're certainly not capable of anything more
complicated.  A cursor is deeply tied into the current state of the
Python Database API, the MySQL server, and whatnot -- all that state
certainly can't be tied up in a string and recreated for the next
request from the browser.

The text you see (<MySQLdb.cursors.Cursor instance at 0x04AC1038>) is
Python's best effor at turning this cursor into a string.  But all it
says is "I had this object here at this address with all this state in
it".  When the request comes back from the browser (milliseconds,
seconds, days later) that object is dead and gone.  Could be Apache
restarted.  MySQL restarted.  The server started smoking and was
replaced.  That cursor is no longer viable.

While I'm on the subject, another good reason not to send things like
this to your client (the browser) is this: you're not sure you'll get
it back.  If you had a way to "reserve" cursors on the server, such
that a browser could indicate "I was using cursor number 243" (perhaps
with an input tag), what would happen if the browser never send another
request?  That cursor would sit around, using up resources.  A couple
dozen idle cursors, and you'll have a DOS'd server..

So the long and short of it is you'll have to open a new cursor for
each request from the web.  It may help to think about the state of the
cursor that's most important to you: not the connection ID to the MySQL
server or anything like that.  It's probably the row number the user
last viewed, along with the components of the query.  If you put *that*
information into the generated HTML in such a way that the browser will
send it back to you (again, <input> tags are good for this), then
you'll be able to create a cursor in the necessary state by grabbing
any old cursor and executing the appropriate query.

I hope this helps.  I try to err on the side of too much explaination.

Dustin

--

  Dustin Mitchell
  dustin at ywlcs.org/djmitche at alumni.uchicago.edu
  http://people.cs.uchicago.edu/~dustin/


More information about the Mod_python mailing list