[python-sybase] Working on the next release

Dave Cole djc at object-craft.com.au
14 Apr 2002 00:23:14 +1000


Just thought I should mention what I a working on for the next release
of the module...

I have decided to try switching to callback style error handling
instead of inline error handling.  This will have a couple of benefits;

1) FreeTDS uses callback error handling.

2) I should pick up server error messages generated while executing
   stored procedures.

The biggest problem is in managing the global interpreter lock (GIL).

Consider the case where there are two threads using the module.
Whenever I call a Sybase API in the extension module I release the GIL
to allow other threads to run while the server does its thing.  Once
the API returns I reacquire the lock before returning to the
interpreter.

With callback error handling the Sybase API will be calling a function
in the extension module while the GIL is released.  This presents a
small problem in that I must reacquire the GIL with the correct thread
state before bouncing the callback up into some Python code.  Consider
the following...

GIL:  Thread 1:                         Thread 2:
      PyThreadState *save1;             PyThreadState *save2;
    
1     save1 = PyThreadState_Swap(NULL);
1     PyEval_ReleaseLock();
2                                       save1 = PyThreadState_Swap(NULL);
2                                       PyEval_ReleaseLock();
-   
-     Sybase API                        Sybase API
-   
1     PyEval_AcquireLock();
1     PyThreadState_Swap(save1);
2                                       PyEval_AcquireLock();
2                                       PyThreadState_Swap(save1);

When both threads are executing a Sybase API and the callback function
is invoked I need to be able to reacquire the GIL using the correct
PyThreadState...

The only way I can see to do this is to only allow a single thread to
operate on a connection at a time.  Before calling the Sybase API I
would store the PyThreadState with my connection structure.  When a
callback is performed by the Sybase API I locate my connection
structure and restore the associated PyThreadState before bouncing the
callback up into the Python code registered for that callback.

I am still studying the Sybase documentation to make sure that the
above theory will work for the callbacks I am interested in handling.

- Dave

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