[Tutor] Python threads vs C threads

Lee-Shanok, Bruce Bruce.Lee-Shanok@cognos.com
Wed, 12 Dec 2001 13:29:14 -0500

I am currently running into a small problem regarding network access calls
and threading.

Basically, I've wrapped a library whose interface closely resembles Berkeley
socket interface such that the calls can be made from within Python.

Now, I have already written a client and server for this interface in C.
What I've done is created a main thread that runs an Accept call, and upon
completion of an Accept call starts another thread send/receive thread and
immediately loops around to call accept again. So:

while (1)
	accept(&connection, socket);
	// make new thread
	srthread = new srthread(connection);

The send/receive thread does just that, sends and receives on that given
connection, while the main thread sits on the accept call waiting for any
new incoming connections.

My problem is that coding this in Python doesn't work, presumably because of
this cooperative threading stuff.. since the Accept is actually a call
outside of Python, what I have is the creation of an sr thread, followed by
the program halting after the second accept (since there is no second
connection incoming)..

Is there any way to circumvent this? Maybe by tricking Python into treating
my Accept call as something that can be interrupted? Or is there a
particular "Python" way to perform this? The issue here is that I need to
use this particular library, so using any existing functionality within
Python that supports sockets isn't an option.


This message may contain privileged and/or confidential information.  If you
have received this e-mail in error or are not the intended recipient, you
may not use, copy, disseminate, or distribute it; do not open any
attachments, delete it immediately from your system and notify the sender by
e-mail promptly that you have done so.  Thank You.