Threading problems at program exit

jepler at unpythonic.net jepler at unpythonic.net
Sat Nov 30 21:46:21 EST 2002


On Sun, Dec 01, 2002 at 09:36:07AM +1100, Dave Cole wrote:
> I should be more specific...
> 
> The code in question is my Python bindings for Sybase.  According the
> the DB-API specification a database module must provide Connection
> objects for managing database connections, and Cursor objects for
> executing commands over a Connection.  The DB-API implies that you
> should be able to do something like the following:
> 
> db = Sybase.connect(...)  # return Connection
> 
> # in thread1
> c1 = db.cursor()
> c1.execute('select * blah')
> while 1:
>     row = c1.fetchone()
>     if not row:
>         break
> 
> # in thread2
> c2 = db.cursor()
> c2.execute('select * from blahblah')
> while 1:
>     row = c2.fetchone()
>     if not row:
>         break
> 
> 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?

Jeff




More information about the Python-list mailing list