From af6963c2f07c9e163278045cd5aeef4356fe2161 Mon Sep 17 00:00:00 2001 From: "Andrew M. Kuchling" Date: Wed, 24 Jan 2001 15:50:19 +0000 Subject: [PATCH] Updated version of asyncore.py from Sam Rushing: Adds support for using select.poll() if it's available Move a 'map is None' test out of an else branch and into the right place --- Lib/asyncore.py | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/Lib/asyncore.py b/Lib/asyncore.py index 8b585d451c0..f177692931a 100644 --- a/Lib/asyncore.py +++ b/Lib/asyncore.py @@ -145,16 +145,52 @@ def poll2 (timeout=0.0, map=None): except KeyError: pass +def poll3 (timeout=0.0, map=None): + # Use the poll() support added to the select module in Python 2.0 + if map is None: + map=socket_map + # timeout is in milliseconds + timeout = int(timeout*1000) + pollster = select.poll() + if map: + l = [] + for fd, obj in map.items(): + flags = 0 + if obj.readable(): + flags = select.POLLIN + if obj.writable(): + flags = flags | select.POLLOUT + if flags: + pollster.register(fd, flags) + r = pollster.poll (timeout) + for fd, flags in r: + try: + obj = map[fd] + try: + if (flags & select.POLLIN): + obj.handle_read_event() + if (flags & select.POLLOUT): + obj.handle_write_event() + except ExitNow: + raise ExitNow + except: + obj.handle_error() + except KeyError: + pass + def loop (timeout=30.0, use_poll=0, map=None): + if map is None: + map=socket_map + if use_poll: - poll_fun = poll2 + if hasattr (select, 'poll'): + poll_fun = poll3 + else: + poll_fun = poll2 else: poll_fun = poll - if map is None: - map=socket_map - while map: poll_fun (timeout, map)