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 select
import socket import socket
import sys import sys
import time
import os import os
from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, \ from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, \
@ -63,52 +64,65 @@ except NameError:
class ExitNow(exceptions.Exception): class ExitNow(exceptions.Exception):
pass 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): def poll(timeout=0.0, map=None):
if map is None: if map is None:
map = socket_map map = socket_map
if map: if map:
r = []; w = []; e = [] r = []; w = []; e = []
for fd, obj in map.iteritems(): for fd, obj in map.items():
if obj.readable(): if obj.readable():
r.append(fd) r.append(fd)
if obj.writable(): if obj.writable():
w.append(fd) w.append(fd)
if [] == r == w == e:
time.sleep(timeout)
else:
try: try:
r, w, e = select.select(r, w, e, timeout) r, w, e = select.select(r, w, e, timeout)
except select.error, err: except select.error, err:
if err[0] != EINTR: if err[0] != EINTR:
raise raise
else:
if DEBUG: return
print r,w,e
for fd in r: for fd in r:
try: obj = map.get(fd)
obj = map[fd] if obj is None:
except KeyError:
continue continue
read(obj)
try:
obj.handle_read_event()
except ExitNow:
raise ExitNow
except:
obj.handle_error()
for fd in w: for fd in w:
try: obj = map.get(fd)
obj = map[fd] if obj is None:
except KeyError:
continue continue
write(obj)
try:
obj.handle_write_event()
except ExitNow:
raise ExitNow
except:
obj.handle_error()
def poll2(timeout=0.0, map=None): def poll2(timeout=0.0, map=None):
import poll import poll
@ -119,7 +133,7 @@ def poll2 (timeout=0.0, map=None):
timeout = int(timeout*1000) timeout = int(timeout*1000)
if map: if map:
l = [] l = []
for fd, obj in map.iteritems(): for fd, obj in map.items():
flags = 0 flags = 0
if obj.readable(): if obj.readable():
flags = poll.POLLIN flags = poll.POLLIN
@ -129,20 +143,10 @@ def poll2 (timeout=0.0, map=None):
l.append((fd, flags)) l.append((fd, flags))
r = poll.poll(l, timeout) r = poll.poll(l, timeout)
for fd, flags in r: for fd, flags in r:
try: obj = map.get(fd)
obj = map[fd] if obj is None:
except KeyError:
continue continue
readwrite(obj, flags)
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()
def poll3(timeout=0.0, map=None): def poll3(timeout=0.0, map=None):
# Use the poll() support added to the select module in Python 2.0 # 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) timeout = int(timeout*1000)
pollster = select.poll() pollster = select.poll()
if map: if map:
for fd, obj in map.iteritems(): for fd, obj in map.items():
flags = 0 flags = 0
if obj.readable(): if obj.readable():
flags = select.POLLIN flags = select.POLLIN
@ -168,23 +172,12 @@ def poll3 (timeout=0.0, map=None):
raise raise
r = [] r = []
for fd, flags in r: for fd, flags in r:
try: obj = map.get(fd)
obj = map[fd] if obj is None:
except KeyError:
continue continue
readwrite(obj, flags)
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()
def loop(timeout=30.0, use_poll=0, map=None): def loop(timeout=30.0, use_poll=0, map=None):
if map is None: if map is None:
map = socket_map map = socket_map
@ -200,6 +193,7 @@ def loop (timeout=30.0, use_poll=0, map=None):
poll_fun(timeout, map) poll_fun(timeout, map)
class dispatcher: class dispatcher:
debug = 0 debug = 0
connected = 0 connected = 0
accepting = 0 accepting = 0
@ -280,7 +274,7 @@ class dispatcher:
# ================================================== # ==================================================
def readable(self): def readable(self):
return 1 return True
if os.name == 'mac': if os.name == 'mac':
# The macintosh will select a listening socket for # The macintosh will select a listening socket for
@ -289,7 +283,7 @@ class dispatcher:
return not self.accepting return not self.accepting
else: else:
def writable(self): def writable(self):
return 1 return True
# ================================================== # ==================================================
# socket object methods. # socket object methods.
@ -308,6 +302,7 @@ class dispatcher:
def connect(self, address): def connect(self, address):
self.connected = 0 self.connected = 0
err = self.socket.connect_ex(address) err = self.socket.connect_ex(address)
# XXX Should interpret Winsock return values
if err in (EINPROGRESS, EALREADY, EWOULDBLOCK): if err in (EINPROGRESS, EALREADY, EWOULDBLOCK):
return return
if err in (0, EISCONN): if err in (0, EISCONN):
@ -318,6 +313,7 @@ class dispatcher:
raise socket.error, err raise socket.error, err
def accept(self): def accept(self):
# XXX can return either an address pair or None
try: try:
conn, addr = self.socket.accept() conn, addr = self.socket.accept()
return conn, addr return conn, addr
@ -365,7 +361,7 @@ class dispatcher:
def __getattr__(self, attr): def __getattr__(self, attr):
return getattr(self.socket, 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 # logging and warning methods. In general, log is for 'hit' logging
# and 'log_info' is for informational, warning and error logging. # and 'log_info' is for informational, warning and error logging.
@ -445,6 +441,7 @@ class dispatcher:
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
class dispatcher_with_send(dispatcher): class dispatcher_with_send(dispatcher):
def __init__(self, sock=None): def __init__(self, sock=None):
dispatcher.__init__(self, sock) dispatcher.__init__(self, sock)
self.out_buffer = '' self.out_buffer = ''
@ -473,80 +470,25 @@ class dispatcher_with_send (dispatcher):
def compact_traceback(): def compact_traceback():
t, v, tb = sys.exc_info() t, v, tb = sys.exc_info()
tbinfo = [] tbinfo = []
while 1: assert tb # Must have a traceback
while tb:
tbinfo.append(( tbinfo.append((
tb.tb_frame.f_code.co_filename, tb.tb_frame.f_code.co_filename,
tb.tb_frame.f_code.co_name, tb.tb_frame.f_code.co_name,
str(tb.tb_lineno) str(tb.tb_lineno)
)) ))
tb = tb.tb_next tb = tb.tb_next
if not tb:
break
# just to be safe # just to be safe
del tb del tb
file, function, line = tbinfo[-1] 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 return (file, function, line), t, v, info
def close_all(map=None): def close_all(map=None):
if map is None: if map is None:
map = socket_map map = socket_map
for x in map.itervalues(): for x in map.values():
x.socket.close() x.socket.close()
map.clear() 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: