Threading problems at program exit

Dave Cole djc at object-craft.com.au
Sat Nov 30 22:14:38 EST 2002


> On Sun, Dec 01, 2002 at 09:36:07AM +1100, Dave Cole wrote:
> 
> > Now the problem is that even though the database connection can be
> > shared between threads it can only support a single query in
> > flight at a time.  This means that while a cursor is fetching
> > results over a Connection I need the Cursor to maintain a lock on
> > the Connection.  I do not have any control over the structure of
> > the client code which is using the Cursor.
> 
> So you must support even the following code, right (two queries in flight
> in the same thread)?
> 
>     c1 = db.cursor()
>     c2 = db.cursor()
>     c1.execute(QUERY1)
>     while 1:
>         row1 = c1.fetchone()
>         if not row1: break
> 
>         c2.execute(QUERY2)
>         while 1:
>             row2 = c2.fetchone()
>             if not row2: break
>             # do something with (row1, row2)        
> 
> If you invent some kind of scheme that involves putting a lock that
> prevents c1 and c2 from being "in a query" at the same time, won't
> this single-threaded code deadlock?

Yes it will deadlock.

The limitations of the feature in a single thread do not prevent it
from being extremely useful in a multi-threaded environment.

If you want to provide a scalable server which does the same query
for a large number of "simultaneous" clients, then the sharing of
connections between threads is very useful.

- Dave

-- 
http://www.object-craft.com.au





More information about the Python-list mailing list