Sign in to follow this  

[Python] 2 threads send and recv on same blocking socket

This topic is 3631 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey! I have a question about sockets in Python (and in general I guess): If I have a client socket that is blocking, waiting for something to come in (a blocking recv() call). Can another thread use the same socket to send something (and vice versa)? I tried with a little sample program and it worked, but I figured it might not be such a good idea in general, or? What's the deal? [Edited by - tufflax on January 7, 2008 3:30:18 AM]

Share this post


Link to post
Share on other sites
That sounds like it could give you a lot of headaches. Threading can get quite dirty already, and a socket doesn't really support reading from it and writing to it simultanously as far as I know.

You should probably take a look at asyncore/asynchat in the python standard library for ways to do this without using threading. Or, alternative, Twisted (twistedmatrix.com).

Share this post


Link to post
Share on other sites
Sockets in general should be thread-safe, although you need to confirm it for your particular implementation.

But as said, Python has Twisted, which should be your choice for networking, and should also solve the problem with blocking.

Share this post


Link to post
Share on other sites

This topic is 3631 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this