Threading problems with SWIG generated code
Yves Perrenoud
nyp at njc.ch
Sun Feb 25 01:26:06 EST 2001
I've ran into some problems when trying to use multiple threads which
make use of modules generated with SWIG. Here's the shortest example I
could come up with to illustrate the problem:
Given the following SWIG interface file:
----
%module blip
unsigned int sleep(unsigned int seconds);
----
I generate the C code using "swig -python", compile it and generate the
shared library.
Here's some sample code using the newly generated "blip" module:
----
import threading, blip
def foo():
blip.sleep(5)
threading.Thread(target=foo).start()
threading.Thread(target=foo).start()
----
You would expect this function to run for a total of approximately 5
seconds, but instead, it runs for 10 seconds! Clearly, the sleep
function calls are running one after the other, when they should be
running in parallel. It's almost as if I was acquiring a lock before the
sleep and releasing it afterwards.
Any idea what's causing this?
..Yves
More information about the Python-list
mailing list