summaryrefslogtreecommitdiffstats
path: root/Lib/asyncore.py
diff options
context:
space:
mode:
authorJeremy Hylton <jeremy@alum.mit.edu>2002-09-08 00:14:54 (GMT)
committerJeremy Hylton <jeremy@alum.mit.edu>2002-09-08 00:14:54 (GMT)
commitd750036b20be5f8ce0a356fe625e8ade3a6c071a (patch)
treefdba831de54735bf41350202f83da0f6265c9784 /Lib/asyncore.py
parent2c0d3224fca38ed07cee2ea9c334d85b2ff0f4fe (diff)
downloadcpython-d750036b20be5f8ce0a356fe625e8ade3a6c071a.zip
cpython-d750036b20be5f8ce0a356fe625e8ade3a6c071a.tar.gz
cpython-d750036b20be5f8ce0a356fe625e8ade3a6c071a.tar.bz2
A little refactoring.
Add read(), write(), and readwrite() helper functions to shorten poll functions. Use get() instead of try/except KeyError for lookup. XXX How could the lookup ever fail? Remove module-level DEBUG flag. Use iteritems() instead of items() when walking the socket map. Reformat the functions I touched so that are consistently Pythonic.
Diffstat (limited to 'Lib/asyncore.py')
-rw-r--r--Lib/asyncore.py106
1 files changed, 46 insertions, 60 deletions
diff --git a/Lib/asyncore.py b/Lib/asyncore.py
index c7a8f12..180a189 100644
--- a/Lib/asyncore.py
+++ b/Lib/asyncore.py
@@ -60,56 +60,63 @@ try:
except NameError:
socket_map = {}
-class ExitNow (exceptions.Exception):
+class ExitNow(exceptions.Exception):
pass
-DEBUG = 0
+def read(obj):
+ try:
+ obj.handle_read_event()
+ except ExitNow:
+ raise
+ except:
+ obj.handle_error()
+
+def write(obj):
+ try:
+ obj.handle_write_event()
+ except ExitNow:
+ raise
+ except:
+ obj.handle_error()
+
+def readwrite(obj, flags):
+ try:
+ if flags & select.POLLIN:
+ obj.handle_read_event()
+ if flags & select.POLLOUT:
+ obj.handle_write_event()
+ except ExitNow:
+ raise
+ except:
+ obj.handle_error()
def poll (timeout=0.0, map=None):
if map is None:
map = socket_map
if map:
r = []; w = []; e = []
- for fd, obj in map.items():
+ for fd, obj in map.iteritems():
if obj.readable():
- r.append (fd)
+ r.append(fd)
if obj.writable():
- w.append (fd)
+ w.append(fd)
try:
- r,w,e = select.select (r,w,e, timeout)
+ r, w, e = select.select(r, w, e, timeout)
except select.error, err:
if err[0] != EINTR:
raise
- r = []; w = []; e = []
-
- if DEBUG:
- print r,w,e
for fd in r:
- try:
- obj = map[fd]
- except KeyError:
+ obj = map.get(fd)
+ if obj is None:
continue
-
- try:
- obj.handle_read_event()
- except ExitNow:
- raise ExitNow
- except:
- obj.handle_error()
+ read(obj)
for fd in w:
- try:
- obj = map[fd]
- except KeyError:
+ obj = map.get(fd)
+ if obj is None:
continue
-
- try:
- obj.handle_write_event()
- except ExitNow:
- raise ExitNow
- except:
- obj.handle_error()
+ write(obj)
def poll2 (timeout=0.0, map=None):
import poll
@@ -120,7 +127,7 @@ def poll2 (timeout=0.0, map=None):
timeout = int(timeout*1000)
if map:
l = []
- for fd, obj in map.items():
+ for fd, obj in map.iteritems():
flags = 0
if obj.readable():
flags = poll.POLLIN
@@ -130,20 +137,10 @@ def poll2 (timeout=0.0, map=None):
l.append ((fd, flags))
r = poll.poll (l, timeout)
for fd, flags in r:
- try:
- obj = map[fd]
- except KeyError:
+ obj = map.get(fd)
+ if obj is None:
continue
-
- try:
- if (flags & poll.POLLIN):
- obj.handle_read_event()
- if (flags & poll.POLLOUT):
- obj.handle_write_event()
- except ExitNow:
- raise ExitNow
- except:
- obj.handle_error()
+ readwrite(obj, flags)
def poll3 (timeout=0.0, map=None):
# Use the poll() support added to the select module in Python 2.0
@@ -154,7 +151,7 @@ def poll3 (timeout=0.0, map=None):
timeout = int(timeout*1000)
pollster = select.poll()
if map:
- for fd, obj in map.items():
+ for fd, obj in map.iteritems():
flags = 0
if obj.readable():
flags = select.POLLIN
@@ -169,25 +166,14 @@ def poll3 (timeout=0.0, map=None):
raise
r = []
for fd, flags in r:
- try:
- obj = map[fd]
- except KeyError:
+ obj = map.get(fd)
+ if obj is None:
continue
-
- 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()
+ readwrite(obj, flags)
def loop (timeout=30.0, use_poll=0, map=None):
-
if map is None:
- map=socket_map
+ map = socket_map
if use_poll:
if hasattr (select, 'poll'):