This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author giampaolo.rodola
Recipients akuchling, djarb, facundobatista, forest, giampaolo.rodola, gvanrossum, intgr, j1m, jafo, josiahcarlson, kevinwatters, mark.dickinson, markb, mcdonc, pitrou, r.david.murray, stutzbach, tseaver
Date 2011-12-14.14:24:37
SpamBayes Score 0.0015763739
Marked as misclassified No
Message-id <1323872678.16.0.0110370948978.issue1641@psf.upfronthosting.co.za>
In-reply-to
Content
Now that I think of it maybe some kind of wrapper would still be necessary.
As of right now, we'd do something like this.
At the core we would have:

import asyncore, asynchat, sched

# global
scheduler = sched.scheduler()

while 1:
    asyncore.loop(timeout=1.0, count=1)  # count=1 makes loop() return after 1 loop
    scheduler.run(blocking=False)       
    

Then, every dispatcher can define a scheduled function of its own:


class Client(asynchat.async_chat):
    # an already connected client 
    # (the "connector" code is not included in this example)

    def __init__(self, *args, **kwargs):
        asynchat.async_chat.__init__(self, *args, **kwargs)
        self.set_terminator("\r\n")
        self.set_timeout()
        
    def set_timeout(self):
        self.timeout = scheduler.enter(30, 0, self.handle_timeout)
        
    def reset_timeout(self):
        scheduler.cancel(self.timeout)
        self.set_timeout()
    
    def found_terminator(self):
        scheduler.cancel(self.timeout)
        self.timeout = scheduler.enter(30, 0, self.handle_timeout)
        # do something with the received data...
        
    def handle_timeout(self):
        self.push("400 connection timed out\r\n")
        self.close()
        
    def close(self):
        scheduler.cancel(self.timeout)
        asynchat.async_chat.close(self)
History
Date User Action Args
2011-12-14 14:24:38giampaolo.rodolasetrecipients: + giampaolo.rodola, gvanrossum, akuchling, facundobatista, jafo, josiahcarlson, tseaver, mark.dickinson, pitrou, forest, kevinwatters, djarb, stutzbach, markb, r.david.murray, intgr, mcdonc, j1m
2011-12-14 14:24:38giampaolo.rodolasetmessageid: <1323872678.16.0.0110370948978.issue1641@psf.upfronthosting.co.za>
2011-12-14 14:24:37giampaolo.rodolalinkissue1641 messages
2011-12-14 14:24:37giampaolo.rodolacreate