2 # Id: asyncore.py,v 2.51 2000/09/07 22:29:26 rushing Exp
3 # Author: Sam Rushing <rushing@nightmare.com>
5 # ======================================================================
6 # Copyright 1996 by Sam Rushing
10 # Permission to use, copy, modify, and distribute this software and
11 # its documentation for any purpose and without fee is hereby
12 # granted, provided that the above copyright notice appear in all
13 # copies and that both that copyright notice and this permission
14 # notice appear in supporting documentation, and that the name of Sam
15 # Rushing not be used in advertising or publicity pertaining to
16 # distribution of the software without specific, written prior
19 # SAM RUSHING DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
20 # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
21 # NO EVENT SHALL SAM RUSHING BE LIABLE FOR ANY SPECIAL, INDIRECT OR
22 # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
23 # OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
24 # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
25 # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 # ======================================================================
28 """Basic infrastructure for asynchronous socket service clients and servers.
30 There are only two ways to have a program on a single processor do "more
31 than one thing at a time". Multi-threaded programming is the simplest and
32 most popular way to do it, but there is another very different technique,
33 that lets you have nearly all the advantages of multi-threading, without
34 actually using multiple threads. it's really only practical if your program
35 is largely I/O bound. If your program is CPU bound, then pre-emptive
36 scheduled threads are probably what you really need. Network servers are
37 rarely CPU-bound, however.
39 If your operating system supports the select() system call in its I/O
40 library (and nearly all do), then you can use it to juggle multiple
41 communication channels at once; doing other work while your I/O is taking
42 place in the "background." Although this strategy can seem strange and
43 complex, especially at first, it is in many ways easier to understand and
44 control than multi-threaded programming. The module documented here solves
45 many of the difficult problems for you, making the task of building
46 sophisticated high-performance network servers and clients a snap.
55 from errno
import EALREADY
, EINPROGRESS
, EWOULDBLOCK
, ECONNRESET
, \
56 ENOTCONN
, ESHUTDOWN
, EINTR
, EISCONN
63 class ExitNow (exceptions
.Exception):
68 def poll (timeout
=0.0, map=None):
72 r
= []; w
= []; e
= []
73 for fd
, obj
in map.items():
79 r
,w
,e
= select
.select (r
,w
,e
, timeout
)
80 except select
.error
, err
:
83 r
= []; w
= []; e
= []
95 obj
.handle_read_event()
108 obj
.handle_write_event()
114 def poll2 (timeout
=0.0, map=None):
118 if timeout
is not None:
119 # timeout is in milliseconds
120 timeout
= int(timeout
*1000)
123 for fd
, obj
in map.items():
128 flags
= flags | poll
.POLLOUT
130 l
.append ((fd
, flags
))
131 r
= poll
.poll (l
, timeout
)
139 if (flags
& poll
.POLLIN
):
140 obj
.handle_read_event()
141 if (flags
& poll
.POLLOUT
):
142 obj
.handle_write_event()
148 def poll3 (timeout
=0.0, map=None):
149 # Use the poll() support added to the select module in Python 2.0
152 if timeout
is not None:
153 # timeout is in milliseconds
154 timeout
= int(timeout
*1000)
155 pollster
= select
.poll()
157 for fd
, obj
in map.items():
160 flags
= select
.POLLIN
162 flags
= flags | select
.POLLOUT
164 pollster
.register(fd
, flags
)
166 r
= pollster
.poll (timeout
)
167 except select
.error
, err
:
178 if (flags
& select
.POLLIN
):
179 obj
.handle_read_event()
180 if (flags
& select
.POLLOUT
):
181 obj
.handle_write_event()
187 def loop (timeout
=30.0, use_poll
=0, map=None):
193 if hasattr (select
, 'poll'):
201 poll_fun (timeout
, map)
210 def __init__ (self
, sock
=None, map=None):
212 self
.set_socket (sock
, map)
213 # I think it should inherit this anyway
214 self
.socket
.setblocking (0)
216 # XXX Does the constructor require that the socket passed
219 self
.addr
= sock
.getpeername()
221 # The addr isn't crucial
227 status
= [self
.__class
__.__module
__+"."+self
.__class
__.__name
__]
228 if self
.accepting
and self
.addr
:
229 status
.append ('listening')
231 status
.append ('connected')
232 if self
.addr
is not None:
234 status
.append ('%s:%d' % self
.addr
)
236 status
.append (repr(self
.addr
))
237 return '<%s at %#x>' % (' '.join (status
), id (self
))
239 def add_channel (self
, map=None):
240 #self.log_info ('adding channel %s' % self)
243 map [self
._fileno
] = self
245 def del_channel (self
, map=None):
250 #self.log_info ('closing channel %d:%s' % (fd, self))
253 def create_socket (self
, family
, type):
254 self
.family_and_type
= family
, type
255 self
.socket
= socket
.socket (family
, type)
256 self
.socket
.setblocking(0)
257 self
._fileno
= self
.socket
.fileno()
260 def set_socket (self
, sock
, map=None):
262 ## self.__dict__['socket'] = sock
263 self
._fileno
= sock
.fileno()
264 self
.add_channel (map)
266 def set_reuse_addr (self
):
267 # try to re-use a server port if possible
269 self
.socket
.setsockopt (
270 socket
.SOL_SOCKET
, socket
.SO_REUSEADDR
,
271 self
.socket
.getsockopt (socket
.SOL_SOCKET
,
272 socket
.SO_REUSEADDR
) |
1
277 # ==================================================
278 # predicates for select()
279 # these are used as filters for the lists of sockets
280 # to pass to select().
281 # ==================================================
287 # The macintosh will select a listening socket for
288 # write if you let it. What might this mean?
290 return not self
.accepting
295 # ==================================================
296 # socket object methods.
297 # ==================================================
299 def listen (self
, num
):
301 if os
.name
== 'nt' and num
> 5:
303 return self
.socket
.listen (num
)
305 def bind (self
, addr
):
307 return self
.socket
.bind (addr
)
309 def connect (self
, address
):
311 err
= self
.socket
.connect_ex(address
)
312 if err
in (EINPROGRESS
, EALREADY
, EWOULDBLOCK
):
314 if err
in (0, EISCONN
):
317 self
.handle_connect()
319 raise socket
.error
, err
322 # XXX can return either an address pair or None
324 conn
, addr
= self
.socket
.accept()
326 except socket
.error
, why
:
327 if why
[0] == EWOULDBLOCK
:
330 raise socket
.error
, why
332 def send (self
, data
):
334 result
= self
.socket
.send (data
)
336 except socket
.error
, why
:
337 if why
[0] == EWOULDBLOCK
:
340 raise socket
.error
, why
343 def recv (self
, buffer_size
):
345 data
= self
.socket
.recv (buffer_size
)
347 # a closed connection is indicated by signaling
348 # a read condition, and having recv() return 0.
353 except socket
.error
, why
:
354 # winsock sometimes throws ENOTCONN
355 if why
[0] in [ECONNRESET
, ENOTCONN
, ESHUTDOWN
]:
359 raise socket
.error
, why
365 # cheap inheritance, used to pass all other attribute
366 # references to the underlying socket object.
367 def __getattr__ (self
, attr
):
368 return getattr (self
.socket
, attr
)
370 # log and log_info maybe overriden to provide more sophisitcated
371 # logging and warning methods. In general, log is for 'hit' logging
372 # and 'log_info' is for informational, warning and error logging.
374 def log (self
, message
):
375 sys
.stderr
.write ('log: %s\n' % str(message
))
377 def log_info (self
, message
, type='info'):
378 if __debug__
or type != 'info':
379 print '%s: %s' % (type, message
)
381 def handle_read_event (self
):
383 # for an accepting socket, getting a read implies
384 # that we are connected
385 if not self
.connected
:
388 elif not self
.connected
:
389 self
.handle_connect()
395 def handle_write_event (self
):
396 # getting a write implies that we are connected
397 if not self
.connected
:
398 self
.handle_connect()
402 def handle_expt_event (self
):
405 def handle_error (self
):
406 nil
, t
, v
, tbinfo
= compact_traceback()
408 # sometimes a user repr method will crash.
410 self_repr
= repr (self
)
412 self_repr
= '<__repr__ (self) failed for object at %0x>' % id(self
)
415 'uncaptured python exception, closing channel %s (%s:%s %s)' % (
425 def handle_expt (self
):
426 self
.log_info ('unhandled exception', 'warning')
428 def handle_read (self
):
429 self
.log_info ('unhandled read event', 'warning')
431 def handle_write (self
):
432 self
.log_info ('unhandled write event', 'warning')
434 def handle_connect (self
):
435 self
.log_info ('unhandled connect event', 'warning')
437 def handle_accept (self
):
438 self
.log_info ('unhandled accept event', 'warning')
440 def handle_close (self
):
441 self
.log_info ('unhandled close event', 'warning')
444 # ---------------------------------------------------------------------------
445 # adds simple buffered output capability, useful for simple clients.
446 # [for more sophisticated usage use asynchat.async_chat]
447 # ---------------------------------------------------------------------------
449 class dispatcher_with_send (dispatcher
):
450 def __init__ (self
, sock
=None):
451 dispatcher
.__init
__ (self
, sock
)
454 def initiate_send (self
):
456 num_sent
= dispatcher
.send (self
, self
.out_buffer
[:512])
457 self
.out_buffer
= self
.out_buffer
[num_sent
:]
459 def handle_write (self
):
463 return (not self
.connected
) or len(self
.out_buffer
)
465 def send (self
, data
):
467 self
.log_info ('sending %s' % repr(data
))
468 self
.out_buffer
= self
.out_buffer
+ data
471 # ---------------------------------------------------------------------------
472 # used for debugging.
473 # ---------------------------------------------------------------------------
475 def compact_traceback ():
476 t
,v
,tb
= sys
.exc_info()
480 tb
.tb_frame
.f_code
.co_filename
,
481 tb
.tb_frame
.f_code
.co_name
,
491 file, function
, line
= tbinfo
[-1]
492 info
= '[' + '] ['.join(map(lambda x
: '|'.join(x
), tbinfo
)) + ']'
493 return (file, function
, line
), t
, v
, info
495 def close_all (map=None):
498 for x
in map.values():
502 # Asynchronous File I/O:
504 # After a little research (reading man pages on various unixen, and
505 # digging through the linux kernel), I've determined that select()
506 # isn't meant for doing doing asynchronous file i/o.
507 # Heartening, though - reading linux/mm/filemap.c shows that linux
508 # supports asynchronous read-ahead. So _MOST_ of the time, the data
509 # will be sitting in memory for us already when we go to read it.
511 # What other OS's (besides NT) support async file i/o? [VMS?]
513 # Regardless, this is useful for pipes, and stdin/stdout...
515 if os
.name
== 'posix':
519 # here we override just enough to make a file
520 # look like a socket for the purposes of asyncore.
521 def __init__ (self
, fd
):
524 def recv (self
, *args
):
525 return os
.read(self
.fd
, *args
)
527 def send (self
, *args
):
528 return os
.write(self
.fd
, *args
)
534 return os
.close (self
.fd
)
539 class file_dispatcher (dispatcher
):
540 def __init__ (self
, fd
):
541 dispatcher
.__init
__ (self
)
543 # set it to non-blocking mode
544 flags
= fcntl
.fcntl (fd
, fcntl
.F_GETFL
, 0)
545 flags
= flags | os
.O_NONBLOCK
546 fcntl
.fcntl (fd
, fcntl
.F_SETFL
, flags
)
549 def set_file (self
, fd
):
551 self
.socket
= file_wrapper (fd
)