473,503 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding idle timeout capabilities to asyncore

Hi there.
We're talking about an asyncore-based server.
Just for the heck of it I'd like to set a timeout which will
disconnects the clients if they're inactive (i.e., no command or data
transfer in progress) for a long period of time.
I was thinking about putting the timeout value into an attribute:

def settimeout(self, secs):
self.timeout = time.time() + secs

And then have the readable method call time.time() at every loop to
check if time has passed or not:

def readable(self):
if time.time() >= self.timeout:
self.send("Timeout")
self.close()
return 1

My only concern is if it's a good idea calling time.time() so often.
Since A LOT of clients could be connected simultaneously, couldn't it
be a too much resource-intensive operation?
I'd also be curious to know how Twisted implemented this kind of
stuff.
By calling time.time() at every loop?
Thanks in advance.

Oct 22 '07 #1
4 4438
On 22 Ott, 12:28, Giampaolo Rodola' <gne...@gmail.comwrote:
Hi there.
We're talking about an asyncore-based server.
Just for the heck of it I'd like to set a timeout which will
disconnects the clients if they're inactive (i.e., no command or data
transfer in progress) for a long period of time.
I was thinking about putting the timeout value into an attribute:

def settimeout(self, secs):
self.timeout = time.time() + secs

And then have the readable method call time.time() at every loop to
check if time has passed or not:

def readable(self):
if time.time() >= self.timeout:
self.send("Timeout")
self.close()
return 1

My only concern is if it's a good idea calling time.time() so often.
Since A LOT of clients could be connected simultaneously, couldn't it
be a too much resource-intensive operation?
I'd also be curious to know how Twisted implemented this kind of
stuff.
By calling time.time() at every loop?

Thanks in advance.
Calling time.time() is relatively inexpensive in comparison to pure
Python function calls, but indeed, it could be a bottleneck.

I don't know what Twisted does, but what I would do is to add two
variables to each instance of the class you want to add timeouts to;
self.timeout and self.lastdata . self.lastdata would hold the time
for the last time you sent or received data on the socket, and
self.timeout would hold the delay between when you last sent/received
data and when it should be closed due to idle timeout.

Now, since you are smart, you don't need to alter your handle_send()
or handle_receive() methods in your asyncore subclass. Instead, you
rewrite asyncore.poll() to update .lastdata for every socket that is
in either the reader or writer list, which will result in one
time.time() call. Further, to check for timeouts, you only ever need
to check those sockets that are *not* in the readable or writable
lists...

To be precise, add the following block to your own copy of
asyncore.poll just after the 'for fd in e:' block...
#handle timeouts
rw = set(r) + set(w)
now = time.time()
for f in (i for i in rw if i in map):
map[f].lastdata = now
for j in (map[i] for i in map if i not in rw):
if j.timeout+j.lastdata now:
#timeout!
j.handle_close()

You ARE going to need to initialize .timeout and .lastdata members for
every instance, but that shouldn't be so bad (for a socket that
doesn't time out, I would actually suggest a 1 hour or 1 day timeout).

- Josiah

Oct 23 '07 #2
On 23 Ott, 17:34, Josiah Carlson <josiah.carl...@gmail.comwrote:
On 22 Ott, 12:28, Giampaolo Rodola' <gne...@gmail.comwrote:


Hi there.
We're talking about an asyncore-based server.
Just for the heck of it I'd like to set a timeout which will
disconnects the clients if they're inactive (i.e., no command or data
transfer in progress) for a long period of time.
I was thinking about putting the timeout value into an attribute:
def settimeout(self, secs):
self.timeout = time.time() + secs
And then have the readable method call time.time() at every loop to
check if time has passed or not:
def readable(self):
if time.time() >= self.timeout:
self.send("Timeout")
self.close()
return 1
My only concern is if it's a good idea calling time.time() so often.
Since A LOT of clients could be connected simultaneously, couldn't it
be a too much resource-intensive operation?
I'd also be curious to know how Twisted implemented this kind of
stuff.
By calling time.time() at every loop?
Thanks in advance.

Calling time.time() is relatively inexpensive in comparison to pure
Python function calls, but indeed, it could be a bottleneck.

I don't know what Twisted does, but what I would do is to add two
variables to each instance of the class you want to add timeouts to;
self.timeout and self.lastdata . self.lastdata would hold the time
for the last time you sent or received data on the socket, and
self.timeout would hold the delay between when you last sent/received
data and when it should be closed due to idle timeout.

Now, since you are smart, you don't need to alter your handle_send()
or handle_receive() methods in your asyncore subclass. Instead, you
rewrite asyncore.poll() to update .lastdata for every socket that is
in either the reader or writer list, which will result in one
time.time() call. Further, to check for timeouts, you only ever need
to check those sockets that are *not* in the readable or writable
lists...

To be precise, add the following block to your own copy of
asyncore.poll just after the 'for fd in e:' block...

#handle timeouts
rw = set(r) + set(w)
now = time.time()
for f in (i for i in rw if i in map):
map[f].lastdata = now
for j in (map[i] for i in map if i not in rw):
if j.timeout+j.lastdata now:
#timeout!
j.handle_close()

You ARE going to need to initialize .timeout and .lastdata members for
every instance, but that shouldn't be so bad (for a socket that
doesn't time out, I would actually suggest a 1 hour or 1 day timeout).

- Josiah- Nascondi testo tra virgolette -

- Mostra testo tra virgolette -
A really nice hack.
Thank you a lot, Josiah.

Oct 24 '07 #3
On Oct 23, 9:30 am, Jean-Paul Calderone <exar...@divmod.comwrote:
On Tue, 23 Oct 2007 15:34:19 -0000, Josiah Carlson <josiah.carl...@gmail.comwrote:
[snip]
Calling time.time() is relatively inexpensive in comparison to pure
Python function calls, but indeed, it could be a bottleneck.

Did you benchmark this on some system?

There isn't really any "pure Python function calls" that can replace
time.time() in a sensible way, so I made up one that does some random
things (including calling another Python function) and compared it to
time.time:

exarkun@charm:~$ python -m timeit -s '
def g():
return 1. + 2 + 3 * 4 - 5 / 6 + 7 ** 8
def f():
return g()
' 'f()'
1000000 loops, best of 3: 1.39 usec per loop
exarkun@charm:~$ python -m timeit -s 'from time import time as f' 'f()'
100000 loops, best of 3: 4.68 usec per loop
exarkun@charm:~$

Not really useful in any real-world sense, but I still wouldn't
characterize time.time as "relatively inexpensive."

Of course, for any real-world work, one would want to profile the
application to determine if removing calls to time.time() could
make a worthwhile difference.
The particular function call he was talking about was the .readable()
or .writable() in an asyncore.dispatcher subclass. Now, the trick
with any nontrivial asyncore.dispatcher subclass is that you don't
want to signal a socket as writable if you don't have any outgoing
buffer. Similarly, I have seen subclasses that follows a state
machine very strictly (no pipelining, you must finish sending before
you can receive, etc.). In that sense, typically, both
the .readable() and .writable() methods will reference an attribute or
two of the instance.

After running some tests, I notice that it seems like a not
insignificant platform difference. Because on my platform (Windows
and Python 2.3)...
>>setup = '''def g(): return 1. + 2 + 3 * 4 - 5 / 6 + 7 ** 8'''
timeit.Timer('g()', setup).repeat()
[2.0143674536171798, 1.9778226522165654, 1.9959138650798764]
>>setup = '''
.... class foo(object):
.... __slots__ = 'a', 'b'
.... def __init__(self):
.... self.a = 1
.... self.b = 2
.... def foo(self):
.... return self.a and self.b
....
.... inst = foo()
.... '''
>>timeit.Timer('inst.foo()', setup).repeat()
[1.4185994550779242, 1.4186812879064519, 1.4478852871044694]
>>timeit.Timer('f()', 'f = lambda:None').repeat()
[0.58938552412047329, 0.58762154196472238, 0.58324245809257036]
>>timeit.Timer('f()', 'from time import time as f').repeat()
[0.68104482574668168, 0.64085672667252425, 0.6558844364245715]

So to answer your question, even though I hadn't bothered to test it,
my beliefs regarding time.time() being fast in relation to the
particular operation he was expecting to perform, at least according
to my own experience on the platform I use the most (Windows and
Python 2.3) was relatively accurate, though indeed it is slower (but
not much in comparison to what those calls would typically do).

Then again, in the subclasses of asyncore that I write, I don't bother
calling .readable() or .writable() at all, each socket adds and
removes itself from a pair of readable and writable dictionaries
automatically, thus removing perhaps hundreds of function calls for
every loop of asyncore.poll .

What these experiments also tell me is that fetching the time on linux
is slow. But really, since I already wrote code that handles *all* of
the timeout handling with a *single* time.time() call, and that also
generally minimizes all explicit function calls, I'm not sure that
your testing examples were ultimately germane to the conversation (how
would one handle timeouts in asyncore). Or maybe it's just late and
I've had a long day :/ .
In any case, I'm glad that I was able to help Giampaolo.
- Josiah

Oct 25 '07 #4
Giampaolo Rodola' <gn****@gmail.comwrites:
def readable(self):
if time.time() >= self.timeout:
self.send("Timeout")
self.close()
return 1
Don't do it this way. Put all the timeouts into a priority queue
(see the heapq module) so you only need to check the one due to expire
soonest.
Oct 25 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5922
by: David M. Wilson | last post by:
Hi peeps, I finally got around recently to doing something serious using Python for serving network clients asynchronously, deciding on asyncore as my starting point. After 2 days or so of...
2
2572
by: Freddie | last post by:
Hi, I've been mangling python-irclib into an asyncore class, so it fits in nicely with the rest of my app. I ran into a problem with asyncore.dispatcher_with_send (Python 2.3.4), though. Not...
0
478
by: Neil Benn | last post by:
Hello, I was wondering if someone could provide assistance. I'm writing a thin wrapper around asyncore for compatibility with a common Comms API that I use. class...
0
1744
by: Tony Meyer | last post by:
Changes in asyncore from 2.3 to 2.4 mean that asyncore.poll() now passes all the sockets in the map to select.select() to be checked for errors, which is probably a good thing. If an error occurs,...
3
3643
by: Jos | last post by:
Hello. I'm using the asyncore and _chat modules to create a network server. I also have, running in a separate thread(s), a "producer" which needs to "push" data onto the network connection(s)....
1
2142
by: Gordon Smith | last post by:
I'm just getting up to speed on Application Pools in IIS6 and I'm wondering if there would be any good reason to stick with the default value of for the Idle Timeout value for Worker processes...
5
1799
by: shumaker | last post by:
I just read an overview of C# 3.0, and it occured to me that some of these features could make it much easier to write programs that automatically make use of multi core processors. After reading...
5
7086
by: JamesHoward | last post by:
I have a problem with python's asyncore module throwing a bad file descriptor error. The code might be difficult to copy here, but the problem is essentially: The server wants to sever the...
6
2086
by: Andreas Tawn | last post by:
I'm trying to integrate the timeout function from here http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/473878 into a long running automation script and the following code causes IDLE after...
0
7202
marktang
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,...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
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...
0
7458
tracyyun
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...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5013
isladogs
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...
0
3167
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...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.