send with timeout socket

S

Stéphane Ninin

Hello,

I have a few questions regarding sockets with timeouts.

Assuming you set a timeout t on a socket s and then call:


1) s.sendall
Is the socket.timeout exception thrown when
not the data was sent in the given time t
or if nothing was sent ?

2) Similar question for s.send:
Is the socket.timeout exception thrown when
nothing was sent or ... ?


What I am actually trying to do is this:

I have a thread which uses a socket to communicate with a socket server,
the socket sends data to the server, but it shouldnot block on the send,
so I want to do something like this:

def sendall(self,data):
while data:
n = self.send(data)
data = data[n:]
try:
self.request.send(data)
except socket.timeout, e:
if self.isTerminated():
return

but I am not sure this would work the way I want.

(self.isTerminated just checks is some event
has been set by another thread)


Thanks for comments/suggestions/answers,
 
S

Steve Holden

Stéphane Ninin said:
Hello,

I have a few questions regarding sockets with timeouts.

Assuming you set a timeout t on a socket s and then call:


1) s.sendall
Is the socket.timeout exception thrown when
not the data was sent in the given time t
or if nothing was sent ?

2) Similar question for s.send:
Is the socket.timeout exception thrown when
nothing was sent or ... ?


What I am actually trying to do is this:

I have a thread which uses a socket to communicate with a socket server,
the socket sends data to the server, but it shouldnot block on the send,
so I want to do something like this:

def sendall(self,data):
while data:
n = self.send(data)
data = data[n:]
try:
self.request.send(data)
except socket.timeout, e:
if self.isTerminated():
return

but I am not sure this would work the way I want.
It wouldn't even compile, as your indentation is shot.

Plus, if the first send attempt (the one before the try) sends all the
data you then try to send an empty string, which seems a little weird: I
don't see the point of the try/except.
(self.isTerminated just checks is some event
has been set by another thread)


Thanks for comments/suggestions/answers,
Sending sockets will usually only block when the sliding window is full
(the sender has sent as much data as the remote receiver has indicated
it can safely receive without loss) and no acknowledgment has been
received from the remote receiver.

Rather than a timeout it would seem sensible, if you don't want the
sending socket to block, to use a non-blocking socket. That way you get
a socket.error if a send would otherwise block.

regards
Steve
 
S

Stéphane Ninin

Also sprach Steve Holden :
It wouldn't even compile, as your indentation is shot.

Plus, if the first send attempt (the one before the try) sends all the
data you then try to send an empty string, which seems a little weird: I
don't see the point of the try/except.


Yes, I typed it *really* too fast, it would be more something like this:

def sendall(self,data):
while data:
try:
n = self.request.send(data)
data = data[n:]
except socket.timeout, e:
if self.isTerminated():
return

Sending sockets will usually only block when the sliding window is full
(the sender has sent as much data as the remote receiver has indicated
it can safely receive without loss) and no acknowledgment has been
received from the remote receiver.

Rather than a timeout it would seem sensible, if you don't want the
sending socket to block, to use a non-blocking socket. That way you get
a socket.error if a send would otherwise block.

I think I need to read more about sockets. :)
 
B

Bryan Olson

Stéphane Ninin said:
I have a few questions regarding sockets with timeouts.

Assuming you set a timeout t on a socket s and then call:


1) s.sendall
Is the socket.timeout exception thrown when
not the data was sent in the given time t
or if nothing was sent ?

Neither; not exactly anyway. It may call send() several
times, and it raises socket.timeout if any of the calls
blocks for longer than the socket's timeout.

2) Similar question for s.send:
Is the socket.timeout exception thrown when
nothing was sent or ... ?

Raised when and only when nothing was sent.
 
B

Bryan Olson

Stéphane Ninin said:
Yes, I typed it *really* too fast, it would be more something like this:

def sendall(self, data):
while data:
try:
n = self.request.send(data)
data = data[n:]
except socket.timeout, e:
if self.isTerminated():
return

And from a previous post:

(self.isTerminated just checks is some event has been set
by another thread)

Do you want to timeout on inactivity, or do you want to timeout
if sendall() does not complete within a given time?
 
S

Stéphane Ninin

Also sprach Bryan Olson :
def sendall(self, data):
while data: try:
n = self.request.send(data)
data = data[n:]
except socket.timeout, e:
if self.isTerminated(): return

And from a previous post:

(self.isTerminated just checks is some event has been set
by another thread)

Do you want to timeout on inactivity, or do you want to timeout
if sendall() does not complete within a given time?

I want to make sure that, if nothing is sent after some time,
if event "terminated" is set,
then the thread doesnot spend more time trying to send something which
has become useless.


So it is more "inactivity" than "a given time":
as long as something is sent, and the thread is not "terminated",
it should keep sending data.

(not sure I am clear enough... :) )
 
B

Bryan Olson

Stéphane Ninin said:
I want to make sure that, if nothing is sent after some time,
if event "terminated" is set,
then the thread doesnot spend more time trying to send something which
has become useless.

You should be good then. The timeout is on each send(), and send
times out if and only if the socket remains not ready for
output.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,217
Latest member
topweb3twitterchannels

Latest Threads

Top