Connecting Tech Pros Worldwide Forums | Help | Site Map

socket read timeout

hg
Guest
 
Posts: n/a
#1: Mar 27 '07
Hi,

I am looking for the most efficient / cleanest way to implement a socket
read with timeout (Windows mainly but would be great if the same code
worked under *nix)

Tanks,

hg


Jarek Zgoda
Guest
 
Posts: n/a
#2: Mar 27 '07

re: socket read timeout


hg napisał(a):
Quote:
I am looking for the most efficient / cleanest way to implement a socket
read with timeout (Windows mainly but would be great if the same code
worked under *nix)
Did you see http://www.timo-tasi.org/python/timeoutsocket.py ?

--
Jarek Zgoda

"We read Knuth so you don't have to."
Steve Holden
Guest
 
Posts: n/a
#3: Mar 27 '07

re: socket read timeout


Jarek Zgoda wrote:
Quote:
hg napisał(a):
>
Quote:
>I am looking for the most efficient / cleanest way to implement a socket
>read with timeout (Windows mainly but would be great if the same code
>worked under *nix)
>
Did you see http://www.timo-tasi.org/python/timeoutsocket.py ?
>
Note that since 2.4, I believe, sockets in the standard library allow
you to specify a timeout parameter.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

skip@pobox.com
Guest
 
Posts: n/a
#4: Mar 27 '07

re: socket read timeout


Quote:
Quote:
>I am looking for the most efficient / cleanest way to implement a
>socket read with timeout (Windows mainly but would be great if the
>same code worked under *nix)
JarekDid you see http://www.timo-tasi.org/python/timeoutsocket.py ?

Also socket objects have timeout attributes now.

Skip

hg
Guest
 
Posts: n/a
#5: Mar 27 '07

re: socket read timeout


Steve Holden wrote:
Quote:
Jarek Zgoda wrote:
Quote:
>hg napisa?(a):
>>
Quote:
>>I am looking for the most efficient / cleanest way to implement a socket
>>read with timeout (Windows mainly but would be great if the same code
>>worked under *nix)
>>
>Did you see http://www.timo-tasi.org/python/timeoutsocket.py ?
>>
Note that since 2.4, I believe, sockets in the standard library allow
you to specify a timeout parameter.
>
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com
Hi and thanks to all.

Do you mean use select ?

Thanks,

hg

Facundo Batista
Guest
 
Posts: n/a
#6: Mar 27 '07

re: socket read timeout


hg wrote:
Quote:
Do you mean use select ?
No, socket's timeout:
Quote:
Quote:
Quote:
>>import socket
>>s = socket.socket()
>>s.settimeout(5)
>>...
Regards,

--
.. Facundo
..
Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/


hg
Guest
 
Posts: n/a
#7: Mar 28 '07

re: socket read timeout


Facundo Batista wrote:
Quote:
hg wrote:
>
Quote:
>Do you mean use select ?
>
No, socket's timeout:
>
Quote:
Quote:
>>>import socket
>>>s = socket.socket()
>>>s.settimeout(5)
>>>...
>
Regards,
>
--
. Facundo
.
Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/

My issue with that is the effect on write: I only want a timeout on read ...
but anyway ...

Thanks,

hg


Steve Holden
Guest
 
Posts: n/a
#8: Mar 28 '07

re: socket read timeout


hg wrote:
Quote:
Facundo Batista wrote:
>
Quote:
>hg wrote:
>>
Quote:
>>Do you mean use select ?
>No, socket's timeout:
>>
Quote:
>>>>import socket
>>>>s = socket.socket()
>>>>s.settimeout(5)
>>>>...
>Regards,
>>
>--
>. Facundo
>.
>Blog: http://www.taniquetil.com.ar/plog/
>PyAr: http://www.python.org/ar/
>
>
My issue with that is the effect on write: I only want a timeout on read ...
but anyway ...
>
If you want selective timeout behaviour then yes, you should use select.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

skip@pobox.com
Guest
 
Posts: n/a
#9: Mar 28 '07

re: socket read timeout



hgMy issue with that is the effect on write: I only want a timeout on
hgread ... but anyway ...

So set a long timeout when you want to write and short timeout when you want
to read.

Skip
hg
Guest
 
Posts: n/a
#10: Mar 28 '07

re: socket read timeout


skip@pobox.com wrote:
Quote:
>
hgMy issue with that is the effect on write: I only want a timeout
on
hgread ... but anyway ...
>
So set a long timeout when you want to write and short timeout when you
want to read.
>
Skip

Not bad .. thanks

Hendrik van Rooyen
Guest
 
Posts: n/a
#11: Mar 29 '07

re: socket read timeout


<skip@p....x.comwrote:

Quote:
>
hgMy issue with that is the effect on write: I only want a timeout on
hgread ... but anyway ...
>
So set a long timeout when you want to write and short timeout when you want
to read.
>
Are sockets full duplex?

I know Ethernet isn't.

- Hendrik


Damjan
Guest
 
Posts: n/a
#12: Mar 30 '07

re: socket read timeout


>So set a long timeout when you want to write and short timeout when you
Quote:
Quote:
>want to read.
>>
>
Are sockets full duplex?
>
I know Ethernet isn't.
Both are full duplex.

--
damjan
Steve Holden
Guest
 
Posts: n/a
#13: Apr 1 '07

re: socket read timeout


Hendrik van Rooyen wrote:
Quote:
<skip@p....x.comwrote:
>
>
Quote:
> hgMy issue with that is the effect on write: I only want a timeout on
> hgread ... but anyway ...
>>
>So set a long timeout when you want to write and short timeout when you want
>to read.
>>
>
Are sockets full duplex?
>
Yes. But you have to use non-blocking calls in your application to use
them as full-duplex in your code.
Quote:
I know Ethernet isn't.
>
Don't know much, then, do you? ;-)

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Hendrik van Rooyen
Guest
 
Posts: n/a
#14: Apr 1 '07

re: socket read timeout


"Steve Holden" <s..e@hol....eb.com>

Quote:
Hendrik van Rooyen wrote:
Quote:
<skip@p....x.comwrote:

Quote:
hgMy issue with that is the effect on write: I only want a timeout on
hgread ... but anyway ...
>
So set a long timeout when you want to write and short timeout when you
want
Quote:
Quote:
Quote:
to read.
>
Are sockets full duplex?
Yes. But you have to use non-blocking calls in your application to use
them as full-duplex in your code.
This seems to bear out the scenario I have described elsewhere in this
thread - I think its caused by the file handlers, but I don't _know_ it.
Quote:
>
Quote:
I know Ethernet isn't.
Don't know much, then, do you? ;-)
No not really - I easily get confused by such things as collisions...

: - )

- Hendrik

Bryan Olson
Guest
 
Posts: n/a
#15: Apr 1 '07

re: socket read timeout


Steve Holden wrote:
Quote:
Hendrik van Rooyen wrote:
Quote:
>Are sockets full duplex?
>>
Yes. But you have to use non-blocking calls in your application to use
them as full-duplex in your code.
Hmmm... I'm missing something. Suppose I have one thread (or
process) reading from a blocking-mode socket while another is
writing to it? What stops it from being full duplex?


--
--Bryan
Hendrik van Rooyen
Guest
 
Posts: n/a
#16: Apr 2 '07

re: socket read timeout


"Bryan Olson" <fakeaddress@nowhere.orgwrote:

Quote:
Steve Holden wrote:
Quote:
Hendrik van Rooyen wrote:
Quote:
Are sockets full duplex?
>
Yes. But you have to use non-blocking calls in your application to use
them as full-duplex in your code.
>
Hmmm... I'm missing something. Suppose I have one thread (or
process) reading from a blocking-mode socket while another is
writing to it? What stops it from being full duplex?
>
Elsewhere in this thread I wrote about my experience with a serial port,
where I can show that the "file handler" only does the write once the
blocking read completes - and the point at issue is if sockets are the same.

We regularly get questions about "my stuff does not come out" on
the group, and I wondered whether this effect is the underlying cause.

But I don't know about the sockets case, which is why I asked.

You raise an interesting point about a different process - my serial
experience is using threads. I have never tried mixing processes
on a serial port. Haven't a clue if its possible, or if the behaviour
will be different.

- Hendrik

Steve Holden
Guest
 
Posts: n/a
#17: Apr 2 '07

re: socket read timeout


Hendrik van Rooyen wrote:
Quote:
"Steve Holden" <s..e@hol....eb.com>
>
>
Quote:
>Hendrik van Rooyen wrote:
Quote:
>> <skip@p....x.comwrote:
>>>
>>>
>>> hgMy issue with that is the effect on write: I only want a timeout on
>>> hgread ... but anyway ...
>>>>
>>>So set a long timeout when you want to write and short timeout when you
want
Quote:
Quote:
>>>to read.
>>>>
>>Are sockets full duplex?
>>>
>Yes. But you have to use non-blocking calls in your application to use
>them as full-duplex in your code.
>
This seems to bear out the scenario I have described elsewhere in this
thread - I think its caused by the file handlers, but I don't _know_ it.
>
Quote:
Quote:
>>I know Ethernet isn't.
>>>
>Don't know much, then, do you? ;-)
>
No not really - I easily get confused by such things as collisions...
>
: - )
Right, but collisions are *so* twentieth-century, aren't they. With a
properly-implemented switched infrastructure Ethernet interfaces can
transmit and receive at the same time.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Steve Holden
Guest
 
Posts: n/a
#18: Apr 2 '07

re: socket read timeout


Bryan Olson wrote:
Quote:
Steve Holden wrote:
Quote:
>Hendrik van Rooyen wrote:
Quote:
>>Are sockets full duplex?
>>>
>Yes. But you have to use non-blocking calls in your application to use
>them as full-duplex in your code.
>
Hmmm... I'm missing something. Suppose I have one thread (or
process) reading from a blocking-mode socket while another is
writing to it? What stops it from being full duplex?
>
>
Nothing, I guess. I just didn't consider threaded code ...

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Hendrik van Rooyen
Guest
 
Posts: n/a
#19: Apr 3 '07

re: socket read timeout


"Steve Holden" <s..e@h...b.comwrote:
Quote:
>
Right, but collisions are *so* twentieth-century, aren't they. With a
properly-implemented switched infrastructure Ethernet interfaces can
transmit and receive at the same time.
This is true, while "A" and "B" are not simultaneously trying to address
"C" - Then you need something like store and forward, on the fly...

: - ) better known as "routing"...

Some (most?) of the little switches I have seen are too dumb even to
allow "A" to talk to "B" while "C" is talking to "D" - they just broadcast
the first "talker"'s message to all the "listeners" - little better than
active hubs, destroying the end point's hardware capability to talk and
listen at the same time.

I think the keywords here are "properly implemented" - its actually not a
trivial problem, as the switch has to know or learn who is where, and set
up paths accordingly, in real time. This is hard to do without store and
forward.

- Hendrik


Closed Thread


Similar Python bytes