How to read and write the same socket in different threads?

Jean-Paul Calderone exarkun at divmod.com
Fri Aug 22 13:58:17 EDT 2008


On Sat, 23 Aug 2008 01:47:23 +0800, Leo Jay <python.leojay at gmail.com> wrote:
>I'd like to read and write the same socket in different threads.
>one thread is only used to read from the socket, and the other is only
>used to write to the socket.
>But I always get a 10022 'Invalid argument' exception. Anyone knows why?
>
>I'm using windows xp.
>
>my source code is here:
>http://pastebin.com/m23e633a2
>

You're connecting and accepting with the same socket.  That's not a very
good thing to do.  You're not even reading and writing on the same socket,
since you're writing to the socket which you get from accept (if the code
could get that far).

What are you trying to do?  Why are you connecting and accepting?  Why do
you need two threads?

Have you seen Twisted?  http://twistedmatrix.com/

Jean-Paul



More information about the Python-list mailing list