is socket thread safe?

Bryan Olson fakeaddress at nowhere.org
Wed Feb 15 18:48:53 EST 2006


e2wugui at gmail.com wrote:
> thread1:
>     while 1:
>         buf = s.read()
>         process(buf)
> 
> thread2:
>     while 1:
>         buf = getdata()
>         s.write(buf)

Sockets don't have read() and write() methods. Connected
sockets have recv() and send()/sendall(). Python's socket
module has a makefile() function, but the resulting file
object wouldn't make sense in the read loop above (and
throws away control that network code typically needs).

Is it safe for one thread to receive from a socket while
another sends over the socket? Yes, that much is safe and
perfectly reasonable.


-- 
--Bryan



More information about the Python-list mailing list