I'm coming from Tcl-world ...

Andreas Leitgeb Andreas.Leitgeb at siemens.at
Mon Aug 5 10:12:31 EDT 2002


Oren Tirosh <oren-py-l at hishome.net> wrote:
> The asyncore documentation could be better. 
I had a look at  help("asyncore"), and it gave me headache :-(

> If you have any ideas how to make it easier to use I'd love to hear them,
> whether they're tcl-inspired or not...
yes, and I think it would be quite easy (up to details) to do 
it in Python:  (the following is python-like, but pseudo-code)
The implementations are missing, and could be based on asyncore.

 module fileevents:
   # register a channel for watching reads, writes or errors
   #   fd (a file-object or a numeric os-filedescriptor)
   #   cb (callback: a function object taking a few parameters
   #         defined later.  Specifying Null here, will cancel
   #         a previously set up callback for that channel
   #   mode (what events to register for: readable,writable,error
   #      or more of them. If error is not registered for an fd
   #      at the time the internal select() returns one for the fd,
   #      then read- and/or write-callback will be called instead
   #      once and then the channel is automtically unregistered.)
   #   data (an arbitrary object, that will be passed to the 
   #      callback as is)
   def register(fd,cb,mode="read",data=None)
   reg=register   # alias :-)

   # if a timer is set, the internally called select is called with 
   #  an appropriate timeout, to awake at the first requested timer
   # after the callback returns, select is automatically called again,
   #  with the time left until the next waiting timer.
   def timer(seconds,cb,data=None,autoreset=0)
   
   # unregister a channel:
   unreg = lambda fd,mode,data=None: register(fd,None,mode,data)

   # the main loop: 
   #   calls select(), and when that finishes, calls all the 
   #   registered callback-functions whose condition is met
   #   and/or all timer-callbacks. Exceptions thrown by callbacks
   #   are caught, and handled: (e.g. printed out, or some
   #   registered exception-handler called)
   #  If there is no timer and no registered fileevent left, 
   #   or on some other condition (yet to be defined - perhaps a 
   #   special exception, as in asyncore), loop() finishes.
   def loop()

So much for the basic interface.
possible enhancements:
  allow identifying & cancelling of timers

Some of the suggested features even go beyond Tcl's current 
fileevents :-)

PS: this was a brainstorming, not yet a thought-out design.
-- 
Newsflash: Sproingy made it to the ground !
  read more ... <http://avl.enemy.org/sproingy/>



More information about the Python-list mailing list