Upgrade to asyncore from 2.3; removed fcntl dependent stuff (since I don't have it on my system).

This commit is contained in:
Jeremy Fincher 2003-04-08 07:04:57 +00:00
parent a07136b07f
commit 51f80c265a
1 changed files with 156 additions and 214 deletions

View File

@ -50,6 +50,7 @@ import exceptions
import select
import socket
import sys
import time
import os
from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, \
@ -63,52 +64,65 @@ except NameError:
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.iteritems():
for fd, obj in map.items():
if obj.readable():
r.append(fd)
if obj.writable():
w.append(fd)
if [] == r == w == e:
time.sleep(timeout)
else:
try:
r, w, e = select.select(r, w, e, timeout)
except select.error, err:
if err[0] != EINTR:
raise
if DEBUG:
print r,w,e
else:
return
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
@ -119,7 +133,7 @@ def poll2 (timeout=0.0, map=None):
timeout = int(timeout*1000)
if map:
l = []
for fd, obj in map.iteritems():
for fd, obj in map.items():
flags = 0
if obj.readable():
flags = poll.POLLIN
@ -129,20 +143,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
@ -153,7 +157,7 @@ def poll3 (timeout=0.0, map=None):
timeout = int(timeout*1000)
pollster = select.poll()
if map:
for fd, obj in map.iteritems():
for fd, obj in map.items():
flags = 0
if obj.readable():
flags = select.POLLIN
@ -168,23 +172,12 @@ 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
@ -200,6 +193,7 @@ def loop (timeout=30.0, use_poll=0, map=None):
poll_fun(timeout, map)
class dispatcher:
debug = 0
connected = 0
accepting = 0
@ -280,7 +274,7 @@ class dispatcher:
# ==================================================
def readable(self):
return 1
return True
if os.name == 'mac':
# The macintosh will select a listening socket for
@ -289,7 +283,7 @@ class dispatcher:
return not self.accepting
else:
def writable(self):
return 1
return True
# ==================================================
# socket object methods.
@ -308,6 +302,7 @@ class dispatcher:
def connect(self, address):
self.connected = 0
err = self.socket.connect_ex(address)
# XXX Should interpret Winsock return values
if err in (EINPROGRESS, EALREADY, EWOULDBLOCK):
return
if err in (0, EISCONN):
@ -318,6 +313,7 @@ class dispatcher:
raise socket.error, err
def accept(self):
# XXX can return either an address pair or None
try:
conn, addr = self.socket.accept()
return conn, addr
@ -365,7 +361,7 @@ class dispatcher:
def __getattr__(self, attr):
return getattr(self.socket, attr)
# log and log_info maybe overriden to provide more sophisitcated
# log and log_info may be overridden to provide more sophisticated
# logging and warning methods. In general, log is for 'hit' logging
# and 'log_info' is for informational, warning and error logging.
@ -445,6 +441,7 @@ class dispatcher:
# ---------------------------------------------------------------------------
class dispatcher_with_send(dispatcher):
def __init__(self, sock=None):
dispatcher.__init__(self, sock)
self.out_buffer = ''
@ -473,80 +470,25 @@ class dispatcher_with_send (dispatcher):
def compact_traceback():
t, v, tb = sys.exc_info()
tbinfo = []
while 1:
assert tb # Must have a traceback
while tb:
tbinfo.append((
tb.tb_frame.f_code.co_filename,
tb.tb_frame.f_code.co_name,
str(tb.tb_lineno)
))
tb = tb.tb_next
if not tb:
break
# just to be safe
del tb
file, function, line = tbinfo[-1]
info = '[' + '] ['.join(map(lambda x: '|'.join(x), tbinfo)) + ']'
info = ' '.join(['[%s|%s|%s]' % x for x in tbinfo])
return (file, function, line), t, v, info
def close_all(map=None):
if map is None:
map = socket_map
for x in map.itervalues():
for x in map.values():
x.socket.close()
map.clear()
# Asynchronous File I/O:
#
# After a little research (reading man pages on various unixen, and
# digging through the linux kernel), I've determined that select()
# isn't meant for doing doing asynchronous file i/o.
# Heartening, though - reading linux/mm/filemap.c shows that linux
# supports asynchronous read-ahead. So _MOST_ of the time, the data
# will be sitting in memory for us already when we go to read it.
#
# What other OS's (besides NT) support async file i/o? [VMS?]
#
# Regardless, this is useful for pipes, and stdin/stdout...
import os
if os.name == 'posix':
import fcntl
class file_wrapper:
# here we override just enough to make a file
# look like a socket for the purposes of asyncore.
def __init__ (self, fd):
self.fd = fd
def recv (self, *args):
return apply (os.read, (self.fd,)+args)
def send (self, *args):
return apply (os.write, (self.fd,)+args)
read = recv
write = send
def close (self):
return os.close (self.fd)
def fileno (self):
return self.fd
class file_dispatcher (dispatcher):
def __init__ (self, fd):
dispatcher.__init__ (self)
self.connected = 1
# set it to non-blocking mode
flags = fcntl.fcntl (fd, fcntl.F_GETFL, 0)
flags = flags | os.O_NONBLOCK
fcntl.fcntl (fd, fcntl.F_SETFL, flags)
self.set_file (fd)
def set_file (self, fd):
self._fileno = fd
self.socket = file_wrapper (fd)
self.add_channel()
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: