thread1:
while 1:
buf = s.read()
process(buf)
thread2:
while 1:
buf = getdata()
s.write(buf) 7 8716 e2*****@gmail.com: [code]
I certainly expect socket to be threadsafe. I use it (via urllib2/httplib)
in a multithreaded program, that runs fine with Python 2.3 and 2.4 on both
Windows XP and Linux.
--
René Pijlman e2*****@gmail.com wrote: thread1: while 1: buf = s.read() process(buf)
thread2: while 1: buf = getdata() s.write(buf)
It is safe, but watch out for this gotcha: If thread B calls
s.close() while thread A is blocked in s.read(), thread A will
never return from the read. My preferred solution is to set
socket timeout to a few seconds, and loop checking a status flag
so I know when to quit.
Steve
Steve Horsley wrote: e2*****@gmail.com wrote:
thread1: while 1: buf = s.read() process(buf)
thread2: while 1: buf = getdata() s.write(buf)
It is safe, but watch out for this gotcha: If thread B calls s.close() while thread A is blocked in s.read(), thread A will never return from the read. My preferred solution is to set socket timeout to a few seconds, and loop checking a status flag so I know when to quit.
I think a better thing would be to use something like a condition object
to tie the two threads together and not use any polling loops.
i.e. consumer goes to sleep while data buffer is empty, producer
produces and signals condition object, consumer wakes up and consumes.
To take this a step further, you have a status flag that is set to
something like QUIT or CONSUME and when the condition is triggered wake
up, then examine the status flag to determine if the consumer should
then quit, consume, or whatever else you'd want your consumer thread to do.
-carl
--
Carl J. Van Arsdall cv*********@mvista.com
Build and Release
MontaVista Software e2*****@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
In article <F5*******************@newssvr29.news.prodigy.net> ,
Bryan Olson <fa*********@nowhere.org> wrote: 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.
I hear it works on most common platforms these days,
anyway. I have seen socket implementations where it
didn't, though - the read would end as if interrupted
by a signal.
Donn Cave, do**@u.washington.edu
Carl J. Van Arsdall wrote: Steve Horsley wrote:
e2*****@gmail.com wrote:
thread1: while 1: buf = s.read() process(buf)
thread2: while 1: buf = getdata() s.write(buf)
It is safe, but watch out for this gotcha: If thread B calls s.close() while thread A is blocked in s.read(), thread A will never return from the read. My preferred solution is to set socket timeout to a few seconds, and loop checking a status flag so I know when to quit.
Certainly one needs timeouts to avoid hanging should the remote
side stop. Sockets don't have a read() method, and hanging on
recv() doesn't seem to have anything to do with close().
I didn't find any definitive doc, so I tested using Python
sockets on Linux (Debian/Ubuntu current) and WinXP. A recv()
started before the close() will block/return just as if
close() were never called. The close() neither triggers recv()
to abort, nor prevents it from receiving data and detecting
shutdown.
I think a better thing would be to use something like a condition object to tie the two threads together and not use any polling loops.
i.e. consumer goes to sleep while data buffer is empty, producer produces and signals condition object, consumer wakes up and consumes.
I can infer two producer-consumer relationships from the example,
but they don't allow a condition object; the writer's consumer and
the reader's producer are on the remote end of the socket. The
socket will already handle the blocking and wake-up.
To take this a step further, you have a status flag that is set to something like QUIT or CONSUME and when the condition is triggered wake up, then examine the status flag to determine if the consumer should then quit, consume, or whatever else you'd want your consumer thread to do.
What problem are you trying to solve? Normal socket sending, receiving,
and shutdown discipline work fine. When the writer is done writing, it
should call sock.shutdown(socket.SHUT_WR). When the reader gets zero
bytes from recv(nonzero), that means the remote end has finished
writing, so the reader may call sock.shutdown(socket.SHUT_RD).
--
--Bryan
Bryan Olson wrote: Carl J. Van Arsdall wrote:
Steve Horsley wrote:
e2*****@gmail.com wrote: thread1: while 1: buf = s.read() process(buf)
thread2: while 1: buf = getdata() s.write(buf)
It is safe, but watch out for this gotcha: If thread B calls s.close() while thread A is blocked in s.read(), thread A will never return from the read. My preferred solution is to set socket timeout to a few seconds, and loop checking a status flag so I know when to quit.
Certainly one needs timeouts to avoid hanging should the remote side stop. Sockets don't have a read() method, and hanging on recv() doesn't seem to have anything to do with close().
I didn't find any definitive doc, so I tested using Python sockets on Linux (Debian/Ubuntu current) and WinXP. A recv() started before the close() will block/return just as if close() were never called. The close() neither triggers recv() to abort, nor prevents it from receiving data and detecting shutdown. I think a better thing would be to use something like a condition object to tie the two threads together and not use any polling loops.
i.e. consumer goes to sleep while data buffer is empty, producer produces and signals condition object, consumer wakes up and consumes.
I can infer two producer-consumer relationships from the example, but they don't allow a condition object; the writer's consumer and the reader's producer are on the remote end of the socket. The socket will already handle the blocking and wake-up.
To take this a step further, you have a status flag that is set to something like QUIT or CONSUME and when the condition is triggered wake up, then examine the status flag to determine if the consumer should then quit, consume, or whatever else you'd want your consumer thread to do.
What problem are you trying to solve? Normal socket sending, receiving, and shutdown discipline work fine. When the writer is done writing, it should call sock.shutdown(socket.SHUT_WR). When the reader gets zero bytes from recv(nonzero), that means the remote end has finished writing, so the reader may call sock.shutdown(socket.SHUT_RD).
Doh! I read the word threads and got carried away not even realizing
sockets. Well, looks like today i'll just have to remember to drink my
coffee
:-D
-c
--
Carl J. Van Arsdall cv*********@mvista.com
Build and Release
MontaVista Software This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Russell E. Owen |
last post by:
I've been doing some socket programming with threads and am confused how
to safely shut down a connection.
First of all, using the file-like object returned by urllib to do ftp
file download...
|
by: Mahesh Devjibhai Dhola [MVP] |
last post by:
Hi,
Socket class documentation says that it is not thread safe. We understand
that if do simultaneous sends on ONE socket then it will be a problem (or
simultaneous receive). Can we create TWO...
|
by: Russell Warren |
last post by:
Does anyone know the scope of the socket.setdefaulttimeout call? Is it
a cross-process/system setting or does it stay local in the application
in which it is called?
I've been testing this and...
|
by: zbcong |
last post by:
Hello:
I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin to listen the socket port,waiting for a...
|
by: Markus S. |
last post by:
Hello!
I have two questions about Socket.Select():
1) How is it possible to interrupt Socket.Select()? I'm calling
Socket.Select() in a worker thread. When new sockets are added (from another...
| |
by: Jonas Hei |
last post by:
Is it safe to call socket.BeginSendTo and socket.BeginSendFrom on a
single instance of Socket from two different threads running
simultaneously?
This is required because I need to listen on a...
|
by: Macca |
last post by:
Hi,
I am implementing an asynchronous socket server in my application. It will
take data from connected clients and put it into a thread safe array that
other threads in my application use.
I...
|
by: roblugt |
last post by:
I have what I imagine is a well-known .Net networking problem, but even
though I've Googled for some time I've not yet come across a thread
where this has been fully explained...
There is a...
|
by: jecheney |
last post by:
Hi,
Im currently using the following code for reading/writing to a network
socket.
private StreamReader clientStreamReader;
private StreamWriter clientStreamWriter;
....
TcpClient tcpClient...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |