blob: 96cc9cc77c2e66c8fb59604f93c3233116e0eb50 [file] [log] [blame]
Fred Drake526a1822000-09-11 04:00:46 +00001# -*- Mode: Python -*-
Tim Peters146965a2001-01-14 18:09:23 +00002# Id: asyncore.py,v 2.51 2000/09/07 22:29:26 rushing Exp
Fred Drake526a1822000-09-11 04:00:46 +00003# Author: Sam Rushing <rushing@nightmare.com>
Guido van Rossum0039d7b1999-01-12 20:19:27 +00004
5# ======================================================================
6# Copyright 1996 by Sam Rushing
Tim Peters146965a2001-01-14 18:09:23 +00007#
Guido van Rossum0039d7b1999-01-12 20:19:27 +00008# All Rights Reserved
Tim Peters146965a2001-01-14 18:09:23 +00009#
Guido van Rossum0039d7b1999-01-12 20:19:27 +000010# 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
17# permission.
Tim Peters146965a2001-01-14 18:09:23 +000018#
Guido van Rossum0039d7b1999-01-12 20:19:27 +000019# 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# ======================================================================
27
Guido van Rossum4b8c6ea2000-02-04 15:39:30 +000028"""Basic infrastructure for asynchronous socket service clients and servers.
29
30There are only two ways to have a program on a single processor do "more
Tim Peters146965a2001-01-14 18:09:23 +000031than one thing at a time". Multi-threaded programming is the simplest and
Guido van Rossum4b8c6ea2000-02-04 15:39:30 +000032most popular way to do it, but there is another very different technique,
33that lets you have nearly all the advantages of multi-threading, without
34actually using multiple threads. it's really only practical if your program
35is largely I/O bound. If your program is CPU bound, then pre-emptive
36scheduled threads are probably what you really need. Network servers are
Tim Peters146965a2001-01-14 18:09:23 +000037rarely CPU-bound, however.
Guido van Rossum4b8c6ea2000-02-04 15:39:30 +000038
Tim Peters146965a2001-01-14 18:09:23 +000039If your operating system supports the select() system call in its I/O
Guido van Rossum4b8c6ea2000-02-04 15:39:30 +000040library (and nearly all do), then you can use it to juggle multiple
41communication channels at once; doing other work while your I/O is taking
42place in the "background." Although this strategy can seem strange and
43complex, especially at first, it is in many ways easier to understand and
44control than multi-threaded programming. The module documented here solves
45many of the difficult problems for you, making the task of building
Tim Peters146965a2001-01-14 18:09:23 +000046sophisticated high-performance network servers and clients a snap.
Guido van Rossum4b8c6ea2000-02-04 15:39:30 +000047"""
48
Andrew M. Kuchlingda85a272000-09-08 20:30:39 +000049import exceptions
Guido van Rossum0039d7b1999-01-12 20:19:27 +000050import select
51import socket
Guido van Rossum0039d7b1999-01-12 20:19:27 +000052import sys
53
54import os
Jeremy Hyltona8b5f7d2001-08-10 14:30:35 +000055from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, \
Jeremy Hyltone16e54f2001-10-29 16:44:37 +000056 ENOTCONN, ESHUTDOWN, EINTR, EISCONN
Guido van Rossum0039d7b1999-01-12 20:19:27 +000057
Andrew M. Kuchlingda85a272000-09-08 20:30:39 +000058try:
Fred Drake526a1822000-09-11 04:00:46 +000059 socket_map
Andrew M. Kuchlingda85a272000-09-08 20:30:39 +000060except NameError:
Fred Drake526a1822000-09-11 04:00:46 +000061 socket_map = {}
Guido van Rossum0039d7b1999-01-12 20:19:27 +000062
Andrew M. Kuchlingda85a272000-09-08 20:30:39 +000063class ExitNow (exceptions.Exception):
Fred Drake526a1822000-09-11 04:00:46 +000064 pass
Andrew M. Kuchlingda85a272000-09-08 20:30:39 +000065
66DEBUG = 0
67
68def poll (timeout=0.0, map=None):
Fred Drake526a1822000-09-11 04:00:46 +000069 if map is None:
70 map = socket_map
71 if map:
72 r = []; w = []; e = []
73 for fd, obj in map.items():
74 if obj.readable():
75 r.append (fd)
76 if obj.writable():
77 w.append (fd)
Jeremy Hyltonfbd57972001-10-29 16:32:19 +000078 try:
79 r,w,e = select.select (r,w,e, timeout)
80 except select.error, err:
81 if err[0] != EINTR:
82 raise
Andrew M. Kuchlingcc5f5b22002-03-08 18:19:59 +000083 r = []; w = []; e = []
Guido van Rossum0039d7b1999-01-12 20:19:27 +000084
Fred Drake526a1822000-09-11 04:00:46 +000085 if DEBUG:
86 print r,w,e
Guido van Rossum0039d7b1999-01-12 20:19:27 +000087
Fred Drake526a1822000-09-11 04:00:46 +000088 for fd in r:
89 try:
90 obj = map[fd]
Fred Drake526a1822000-09-11 04:00:46 +000091 except KeyError:
Martin v. Löwis6ec9a362001-10-09 10:10:33 +000092 continue
93
94 try:
95 obj.handle_read_event()
96 except ExitNow:
97 raise ExitNow
98 except:
99 obj.handle_error()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000100
Fred Drake526a1822000-09-11 04:00:46 +0000101 for fd in w:
102 try:
103 obj = map[fd]
Fred Drake526a1822000-09-11 04:00:46 +0000104 except KeyError:
Martin v. Löwis6ec9a362001-10-09 10:10:33 +0000105 continue
106
107 try:
108 obj.handle_write_event()
109 except ExitNow:
110 raise ExitNow
111 except:
112 obj.handle_error()
Andrew M. Kuchlingda85a272000-09-08 20:30:39 +0000113
114def poll2 (timeout=0.0, map=None):
Fred Drake526a1822000-09-11 04:00:46 +0000115 import poll
116 if map is None:
117 map=socket_map
Martin v. Löwisf6cc07c2001-09-19 17:31:47 +0000118 if timeout is not None:
119 # timeout is in milliseconds
120 timeout = int(timeout*1000)
Fred Drake526a1822000-09-11 04:00:46 +0000121 if map:
122 l = []
123 for fd, obj in map.items():
124 flags = 0
125 if obj.readable():
126 flags = poll.POLLIN
127 if obj.writable():
128 flags = flags | poll.POLLOUT
129 if flags:
130 l.append ((fd, flags))
131 r = poll.poll (l, timeout)
132 for fd, flags in r:
133 try:
134 obj = map[fd]
Fred Drake526a1822000-09-11 04:00:46 +0000135 except KeyError:
Martin v. Löwis6ec9a362001-10-09 10:10:33 +0000136 continue
137
138 try:
139 if (flags & poll.POLLIN):
140 obj.handle_read_event()
141 if (flags & poll.POLLOUT):
142 obj.handle_write_event()
143 except ExitNow:
144 raise ExitNow
145 except:
146 obj.handle_error()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000147
Andrew M. Kuchlingaf6963c2001-01-24 15:50:19 +0000148def poll3 (timeout=0.0, map=None):
149 # Use the poll() support added to the select module in Python 2.0
150 if map is None:
151 map=socket_map
Martin v. Löwisf6cc07c2001-09-19 17:31:47 +0000152 if timeout is not None:
153 # timeout is in milliseconds
154 timeout = int(timeout*1000)
Andrew M. Kuchlingaf6963c2001-01-24 15:50:19 +0000155 pollster = select.poll()
156 if map:
Andrew M. Kuchlingaf6963c2001-01-24 15:50:19 +0000157 for fd, obj in map.items():
158 flags = 0
159 if obj.readable():
160 flags = select.POLLIN
161 if obj.writable():
162 flags = flags | select.POLLOUT
163 if flags:
164 pollster.register(fd, flags)
Jeremy Hyltonfbd57972001-10-29 16:32:19 +0000165 try:
166 r = pollster.poll (timeout)
167 except select.error, err:
168 if err[0] != EINTR:
169 raise
170 r = []
Andrew M. Kuchlingaf6963c2001-01-24 15:50:19 +0000171 for fd, flags in r:
172 try:
173 obj = map[fd]
Andrew M. Kuchlingaf6963c2001-01-24 15:50:19 +0000174 except KeyError:
Martin v. Löwis6ec9a362001-10-09 10:10:33 +0000175 continue
176
177 try:
178 if (flags & select.POLLIN):
179 obj.handle_read_event()
180 if (flags & select.POLLOUT):
181 obj.handle_write_event()
182 except ExitNow:
183 raise ExitNow
184 except:
185 obj.handle_error()
Andrew M. Kuchlingaf6963c2001-01-24 15:50:19 +0000186
Andrew M. Kuchlingda85a272000-09-08 20:30:39 +0000187def loop (timeout=30.0, use_poll=0, map=None):
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000188
Andrew M. Kuchlingaf6963c2001-01-24 15:50:19 +0000189 if map is None:
190 map=socket_map
191
Fred Drake526a1822000-09-11 04:00:46 +0000192 if use_poll:
Andrew M. Kuchlingaf6963c2001-01-24 15:50:19 +0000193 if hasattr (select, 'poll'):
194 poll_fun = poll3
195 else:
196 poll_fun = poll2
Fred Drake526a1822000-09-11 04:00:46 +0000197 else:
198 poll_fun = poll
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000199
Fred Drake526a1822000-09-11 04:00:46 +0000200 while map:
201 poll_fun (timeout, map)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000202
203class dispatcher:
Fred Drake526a1822000-09-11 04:00:46 +0000204 debug = 0
205 connected = 0
206 accepting = 0
207 closing = 0
208 addr = None
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000209
Fred Drake526a1822000-09-11 04:00:46 +0000210 def __init__ (self, sock=None, map=None):
211 if sock:
212 self.set_socket (sock, map)
213 # I think it should inherit this anyway
214 self.socket.setblocking (0)
215 self.connected = 1
Jeremy Hylton2a05bc72001-12-14 16:15:11 +0000216 # XXX Does the constructor require that the socket passed
217 # be connected?
218 try:
219 self.addr = sock.getpeername()
220 except socket.error:
221 # The addr isn't crucial
222 pass
Jeremy Hyltonfbd57972001-10-29 16:32:19 +0000223 else:
224 self.socket = None
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000225
Fred Drake526a1822000-09-11 04:00:46 +0000226 def __repr__ (self):
Martin v. Löwis29103c72001-10-18 17:33:19 +0000227 status = [self.__class__.__module__+"."+self.__class__.__name__]
228 if self.accepting and self.addr:
229 status.append ('listening')
230 elif self.connected:
231 status.append ('connected')
232 if self.addr is not None:
233 try:
234 status.append ('%s:%d' % self.addr)
235 except TypeError:
236 status.append (repr(self.addr))
237 return '<%s at %#x>' % (' '.join (status), id (self))
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000238
Fred Drake526a1822000-09-11 04:00:46 +0000239 def add_channel (self, map=None):
240 #self.log_info ('adding channel %s' % self)
241 if map is None:
242 map=socket_map
243 map [self._fileno] = self
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000244
Fred Drake526a1822000-09-11 04:00:46 +0000245 def del_channel (self, map=None):
246 fd = self._fileno
247 if map is None:
248 map=socket_map
249 if map.has_key (fd):
250 #self.log_info ('closing channel %d:%s' % (fd, self))
251 del map [fd]
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000252
Fred Drake526a1822000-09-11 04:00:46 +0000253 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()
258 self.add_channel()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000259
Fred Drake526a1822000-09-11 04:00:46 +0000260 def set_socket (self, sock, map=None):
Jeremy Hyltonfbd57972001-10-29 16:32:19 +0000261 self.socket = sock
262## self.__dict__['socket'] = sock
Fred Drake526a1822000-09-11 04:00:46 +0000263 self._fileno = sock.fileno()
264 self.add_channel (map)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000265
Fred Drake526a1822000-09-11 04:00:46 +0000266 def set_reuse_addr (self):
267 # try to re-use a server port if possible
268 try:
269 self.socket.setsockopt (
270 socket.SOL_SOCKET, socket.SO_REUSEADDR,
Jeremy Hyltona8b5f7d2001-08-10 14:30:35 +0000271 self.socket.getsockopt (socket.SOL_SOCKET,
272 socket.SO_REUSEADDR) | 1
Fred Drake526a1822000-09-11 04:00:46 +0000273 )
Fred Drake9f9b5932001-05-11 18:28:54 +0000274 except socket.error:
Fred Drake526a1822000-09-11 04:00:46 +0000275 pass
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000276
Fred Drake526a1822000-09-11 04:00:46 +0000277 # ==================================================
278 # predicates for select()
279 # these are used as filters for the lists of sockets
280 # to pass to select().
281 # ==================================================
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000282
Fred Drake526a1822000-09-11 04:00:46 +0000283 def readable (self):
284 return 1
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000285
Fred Drake526a1822000-09-11 04:00:46 +0000286 if os.name == 'mac':
287 # The macintosh will select a listening socket for
288 # write if you let it. What might this mean?
289 def writable (self):
290 return not self.accepting
291 else:
292 def writable (self):
293 return 1
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000294
Fred Drake526a1822000-09-11 04:00:46 +0000295 # ==================================================
296 # socket object methods.
297 # ==================================================
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000298
Fred Drake526a1822000-09-11 04:00:46 +0000299 def listen (self, num):
300 self.accepting = 1
301 if os.name == 'nt' and num > 5:
302 num = 1
303 return self.socket.listen (num)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000304
Fred Drake526a1822000-09-11 04:00:46 +0000305 def bind (self, addr):
306 self.addr = addr
307 return self.socket.bind (addr)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000308
Fred Drake526a1822000-09-11 04:00:46 +0000309 def connect (self, address):
310 self.connected = 0
Jeremy Hyltone16e54f2001-10-29 16:44:37 +0000311 err = self.socket.connect_ex(address)
312 if err in (EINPROGRESS, EALREADY, EWOULDBLOCK):
313 return
314 if err in (0, EISCONN):
315 self.addr = address
316 self.connected = 1
317 self.handle_connect()
Jeremy Hyltonf24339f2001-10-30 14:16:17 +0000318 else:
319 raise socket.error, err
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000320
Fred Drake526a1822000-09-11 04:00:46 +0000321 def accept (self):
322 try:
323 conn, addr = self.socket.accept()
324 return conn, addr
325 except socket.error, why:
326 if why[0] == EWOULDBLOCK:
327 pass
328 else:
329 raise socket.error, why
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000330
Fred Drake526a1822000-09-11 04:00:46 +0000331 def send (self, data):
332 try:
333 result = self.socket.send (data)
334 return result
335 except socket.error, why:
336 if why[0] == EWOULDBLOCK:
337 return 0
338 else:
339 raise socket.error, why
340 return 0
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000341
Fred Drake526a1822000-09-11 04:00:46 +0000342 def recv (self, buffer_size):
343 try:
344 data = self.socket.recv (buffer_size)
345 if not data:
346 # a closed connection is indicated by signaling
347 # a read condition, and having recv() return 0.
348 self.handle_close()
349 return ''
350 else:
351 return data
352 except socket.error, why:
353 # winsock sometimes throws ENOTCONN
354 if why[0] in [ECONNRESET, ENOTCONN, ESHUTDOWN]:
355 self.handle_close()
356 return ''
357 else:
358 raise socket.error, why
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000359
Fred Drake526a1822000-09-11 04:00:46 +0000360 def close (self):
361 self.del_channel()
362 self.socket.close()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000363
Fred Drake526a1822000-09-11 04:00:46 +0000364 # cheap inheritance, used to pass all other attribute
365 # references to the underlying socket object.
366 def __getattr__ (self, attr):
367 return getattr (self.socket, attr)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000368
Fred Drake526a1822000-09-11 04:00:46 +0000369 # log and log_info maybe overriden to provide more sophisitcated
370 # logging and warning methods. In general, log is for 'hit' logging
Tim Peters146965a2001-01-14 18:09:23 +0000371 # and 'log_info' is for informational, warning and error logging.
Andrew M. Kuchlingda85a272000-09-08 20:30:39 +0000372
Fred Drake526a1822000-09-11 04:00:46 +0000373 def log (self, message):
374 sys.stderr.write ('log: %s\n' % str(message))
Andrew M. Kuchlingda85a272000-09-08 20:30:39 +0000375
Fred Drake526a1822000-09-11 04:00:46 +0000376 def log_info (self, message, type='info'):
377 if __debug__ or type != 'info':
378 print '%s: %s' % (type, message)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000379
Fred Drake526a1822000-09-11 04:00:46 +0000380 def handle_read_event (self):
381 if self.accepting:
382 # for an accepting socket, getting a read implies
383 # that we are connected
384 if not self.connected:
385 self.connected = 1
386 self.handle_accept()
387 elif not self.connected:
388 self.handle_connect()
389 self.connected = 1
390 self.handle_read()
391 else:
392 self.handle_read()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000393
Fred Drake526a1822000-09-11 04:00:46 +0000394 def handle_write_event (self):
395 # getting a write implies that we are connected
396 if not self.connected:
397 self.handle_connect()
398 self.connected = 1
399 self.handle_write()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000400
Fred Drake526a1822000-09-11 04:00:46 +0000401 def handle_expt_event (self):
402 self.handle_expt()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000403
Fred Drake526a1822000-09-11 04:00:46 +0000404 def handle_error (self):
Jeremy Hyltona8b5f7d2001-08-10 14:30:35 +0000405 nil, t, v, tbinfo = compact_traceback()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000406
Fred Drake526a1822000-09-11 04:00:46 +0000407 # sometimes a user repr method will crash.
408 try:
409 self_repr = repr (self)
410 except:
411 self_repr = '<__repr__ (self) failed for object at %0x>' % id(self)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000412
Fred Drake526a1822000-09-11 04:00:46 +0000413 self.log_info (
414 'uncaptured python exception, closing channel %s (%s:%s %s)' % (
415 self_repr,
416 t,
417 v,
418 tbinfo
419 ),
420 'error'
421 )
422 self.close()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000423
Fred Drake526a1822000-09-11 04:00:46 +0000424 def handle_expt (self):
425 self.log_info ('unhandled exception', 'warning')
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000426
Fred Drake526a1822000-09-11 04:00:46 +0000427 def handle_read (self):
428 self.log_info ('unhandled read event', 'warning')
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000429
Fred Drake526a1822000-09-11 04:00:46 +0000430 def handle_write (self):
431 self.log_info ('unhandled write event', 'warning')
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000432
Fred Drake526a1822000-09-11 04:00:46 +0000433 def handle_connect (self):
434 self.log_info ('unhandled connect event', 'warning')
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000435
Fred Drake526a1822000-09-11 04:00:46 +0000436 def handle_accept (self):
437 self.log_info ('unhandled accept event', 'warning')
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000438
Fred Drake526a1822000-09-11 04:00:46 +0000439 def handle_close (self):
440 self.log_info ('unhandled close event', 'warning')
441 self.close()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000442
443# ---------------------------------------------------------------------------
444# adds simple buffered output capability, useful for simple clients.
445# [for more sophisticated usage use asynchat.async_chat]
446# ---------------------------------------------------------------------------
447
448class dispatcher_with_send (dispatcher):
Fred Drake526a1822000-09-11 04:00:46 +0000449 def __init__ (self, sock=None):
450 dispatcher.__init__ (self, sock)
451 self.out_buffer = ''
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000452
Fred Drake526a1822000-09-11 04:00:46 +0000453 def initiate_send (self):
454 num_sent = 0
455 num_sent = dispatcher.send (self, self.out_buffer[:512])
456 self.out_buffer = self.out_buffer[num_sent:]
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000457
Fred Drake526a1822000-09-11 04:00:46 +0000458 def handle_write (self):
459 self.initiate_send()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000460
Fred Drake526a1822000-09-11 04:00:46 +0000461 def writable (self):
462 return (not self.connected) or len(self.out_buffer)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000463
Fred Drake526a1822000-09-11 04:00:46 +0000464 def send (self, data):
465 if self.debug:
466 self.log_info ('sending %s' % repr(data))
467 self.out_buffer = self.out_buffer + data
468 self.initiate_send()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000469
470# ---------------------------------------------------------------------------
471# used for debugging.
472# ---------------------------------------------------------------------------
473
Guido van Rossuma8d0f4f1999-06-08 13:20:05 +0000474def compact_traceback ():
Fred Drake526a1822000-09-11 04:00:46 +0000475 t,v,tb = sys.exc_info()
476 tbinfo = []
477 while 1:
478 tbinfo.append ((
479 tb.tb_frame.f_code.co_filename,
Tim Peters146965a2001-01-14 18:09:23 +0000480 tb.tb_frame.f_code.co_name,
Fred Drake526a1822000-09-11 04:00:46 +0000481 str(tb.tb_lineno)
482 ))
483 tb = tb.tb_next
484 if not tb:
485 break
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000486
Fred Drake526a1822000-09-11 04:00:46 +0000487 # just to be safe
488 del tb
Guido van Rossuma8d0f4f1999-06-08 13:20:05 +0000489
Fred Drake526a1822000-09-11 04:00:46 +0000490 file, function, line = tbinfo[-1]
Eric S. Raymondb49f4a42001-02-09 05:07:04 +0000491 info = '[' + '] ['.join(map(lambda x: '|'.join(x), tbinfo)) + ']'
Fred Drake526a1822000-09-11 04:00:46 +0000492 return (file, function, line), t, v, info
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000493
Andrew M. Kuchlingda85a272000-09-08 20:30:39 +0000494def close_all (map=None):
Fred Drake526a1822000-09-11 04:00:46 +0000495 if map is None:
496 map=socket_map
497 for x in map.values():
498 x.socket.close()
499 map.clear()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000500
501# Asynchronous File I/O:
502#
503# After a little research (reading man pages on various unixen, and
504# digging through the linux kernel), I've determined that select()
505# isn't meant for doing doing asynchronous file i/o.
506# Heartening, though - reading linux/mm/filemap.c shows that linux
507# supports asynchronous read-ahead. So _MOST_ of the time, the data
508# will be sitting in memory for us already when we go to read it.
509#
510# What other OS's (besides NT) support async file i/o? [VMS?]
511#
512# Regardless, this is useful for pipes, and stdin/stdout...
513
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000514if os.name == 'posix':
Fred Drake526a1822000-09-11 04:00:46 +0000515 import fcntl
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000516
Fred Drake526a1822000-09-11 04:00:46 +0000517 class file_wrapper:
518 # here we override just enough to make a file
519 # look like a socket for the purposes of asyncore.
520 def __init__ (self, fd):
521 self.fd = fd
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000522
Fred Drake526a1822000-09-11 04:00:46 +0000523 def recv (self, *args):
524 return apply (os.read, (self.fd,)+args)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000525
Fred Drake526a1822000-09-11 04:00:46 +0000526 def send (self, *args):
527 return apply (os.write, (self.fd,)+args)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000528
Fred Drake526a1822000-09-11 04:00:46 +0000529 read = recv
530 write = send
Andrew M. Kuchlingda85a272000-09-08 20:30:39 +0000531
Fred Drake526a1822000-09-11 04:00:46 +0000532 def close (self):
533 return os.close (self.fd)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000534
Fred Drake526a1822000-09-11 04:00:46 +0000535 def fileno (self):
536 return self.fd
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000537
Fred Drake526a1822000-09-11 04:00:46 +0000538 class file_dispatcher (dispatcher):
539 def __init__ (self, fd):
540 dispatcher.__init__ (self)
541 self.connected = 1
542 # set it to non-blocking mode
Fred Drakea94414a2001-05-10 15:33:31 +0000543 flags = fcntl.fcntl (fd, fcntl.F_GETFL, 0)
544 flags = flags | os.O_NONBLOCK
545 fcntl.fcntl (fd, fcntl.F_SETFL, flags)
Fred Drake526a1822000-09-11 04:00:46 +0000546 self.set_file (fd)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000547
Fred Drake526a1822000-09-11 04:00:46 +0000548 def set_file (self, fd):
549 self._fileno = fd
550 self.socket = file_wrapper (fd)
551 self.add_channel()