blob: 6bbfbabd589bbf3659107764ffb3f053bd6743e5 [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):
Jeremy Hyltonf32e4592002-04-04 21:02:24 +0000322 # XXX can return either an address pair or None
Fred Drake526a1822000-09-11 04:00:46 +0000323 try:
324 conn, addr = self.socket.accept()
325 return conn, addr
326 except socket.error, why:
327 if why[0] == EWOULDBLOCK:
328 pass
329 else:
330 raise socket.error, why
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000331
Fred Drake526a1822000-09-11 04:00:46 +0000332 def send (self, data):
333 try:
334 result = self.socket.send (data)
335 return result
336 except socket.error, why:
337 if why[0] == EWOULDBLOCK:
338 return 0
339 else:
340 raise socket.error, why
341 return 0
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000342
Fred Drake526a1822000-09-11 04:00:46 +0000343 def recv (self, buffer_size):
344 try:
345 data = self.socket.recv (buffer_size)
346 if not data:
347 # a closed connection is indicated by signaling
348 # a read condition, and having recv() return 0.
349 self.handle_close()
350 return ''
351 else:
352 return data
353 except socket.error, why:
354 # winsock sometimes throws ENOTCONN
355 if why[0] in [ECONNRESET, ENOTCONN, ESHUTDOWN]:
356 self.handle_close()
357 return ''
358 else:
359 raise socket.error, why
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000360
Fred Drake526a1822000-09-11 04:00:46 +0000361 def close (self):
362 self.del_channel()
363 self.socket.close()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000364
Fred Drake526a1822000-09-11 04:00:46 +0000365 # 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)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000369
Fred Drake526a1822000-09-11 04:00:46 +0000370 # log and log_info maybe overriden to provide more sophisitcated
371 # logging and warning methods. In general, log is for 'hit' logging
Tim Peters146965a2001-01-14 18:09:23 +0000372 # and 'log_info' is for informational, warning and error logging.
Andrew M. Kuchlingda85a272000-09-08 20:30:39 +0000373
Fred Drake526a1822000-09-11 04:00:46 +0000374 def log (self, message):
375 sys.stderr.write ('log: %s\n' % str(message))
Andrew M. Kuchlingda85a272000-09-08 20:30:39 +0000376
Fred Drake526a1822000-09-11 04:00:46 +0000377 def log_info (self, message, type='info'):
378 if __debug__ or type != 'info':
379 print '%s: %s' % (type, message)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000380
Fred Drake526a1822000-09-11 04:00:46 +0000381 def handle_read_event (self):
382 if self.accepting:
383 # for an accepting socket, getting a read implies
384 # that we are connected
385 if not self.connected:
386 self.connected = 1
387 self.handle_accept()
388 elif not self.connected:
389 self.handle_connect()
390 self.connected = 1
391 self.handle_read()
392 else:
393 self.handle_read()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000394
Fred Drake526a1822000-09-11 04:00:46 +0000395 def handle_write_event (self):
396 # getting a write implies that we are connected
397 if not self.connected:
398 self.handle_connect()
399 self.connected = 1
400 self.handle_write()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000401
Fred Drake526a1822000-09-11 04:00:46 +0000402 def handle_expt_event (self):
403 self.handle_expt()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000404
Fred Drake526a1822000-09-11 04:00:46 +0000405 def handle_error (self):
Jeremy Hyltona8b5f7d2001-08-10 14:30:35 +0000406 nil, t, v, tbinfo = compact_traceback()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000407
Fred Drake526a1822000-09-11 04:00:46 +0000408 # sometimes a user repr method will crash.
409 try:
410 self_repr = repr (self)
411 except:
412 self_repr = '<__repr__ (self) failed for object at %0x>' % id(self)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000413
Fred Drake526a1822000-09-11 04:00:46 +0000414 self.log_info (
415 'uncaptured python exception, closing channel %s (%s:%s %s)' % (
416 self_repr,
417 t,
418 v,
419 tbinfo
420 ),
421 'error'
422 )
423 self.close()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000424
Fred Drake526a1822000-09-11 04:00:46 +0000425 def handle_expt (self):
426 self.log_info ('unhandled exception', 'warning')
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000427
Fred Drake526a1822000-09-11 04:00:46 +0000428 def handle_read (self):
429 self.log_info ('unhandled read event', 'warning')
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000430
Fred Drake526a1822000-09-11 04:00:46 +0000431 def handle_write (self):
432 self.log_info ('unhandled write event', 'warning')
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000433
Fred Drake526a1822000-09-11 04:00:46 +0000434 def handle_connect (self):
435 self.log_info ('unhandled connect event', 'warning')
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000436
Fred Drake526a1822000-09-11 04:00:46 +0000437 def handle_accept (self):
438 self.log_info ('unhandled accept event', 'warning')
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000439
Fred Drake526a1822000-09-11 04:00:46 +0000440 def handle_close (self):
441 self.log_info ('unhandled close event', 'warning')
442 self.close()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000443
444# ---------------------------------------------------------------------------
445# adds simple buffered output capability, useful for simple clients.
446# [for more sophisticated usage use asynchat.async_chat]
447# ---------------------------------------------------------------------------
448
449class dispatcher_with_send (dispatcher):
Fred Drake526a1822000-09-11 04:00:46 +0000450 def __init__ (self, sock=None):
451 dispatcher.__init__ (self, sock)
452 self.out_buffer = ''
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000453
Fred Drake526a1822000-09-11 04:00:46 +0000454 def initiate_send (self):
455 num_sent = 0
456 num_sent = dispatcher.send (self, self.out_buffer[:512])
457 self.out_buffer = self.out_buffer[num_sent:]
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000458
Fred Drake526a1822000-09-11 04:00:46 +0000459 def handle_write (self):
460 self.initiate_send()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000461
Fred Drake526a1822000-09-11 04:00:46 +0000462 def writable (self):
463 return (not self.connected) or len(self.out_buffer)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000464
Fred Drake526a1822000-09-11 04:00:46 +0000465 def send (self, data):
466 if self.debug:
467 self.log_info ('sending %s' % repr(data))
468 self.out_buffer = self.out_buffer + data
469 self.initiate_send()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000470
471# ---------------------------------------------------------------------------
472# used for debugging.
473# ---------------------------------------------------------------------------
474
Guido van Rossuma8d0f4f1999-06-08 13:20:05 +0000475def compact_traceback ():
Fred Drake526a1822000-09-11 04:00:46 +0000476 t,v,tb = sys.exc_info()
477 tbinfo = []
478 while 1:
479 tbinfo.append ((
480 tb.tb_frame.f_code.co_filename,
Tim Peters146965a2001-01-14 18:09:23 +0000481 tb.tb_frame.f_code.co_name,
Fred Drake526a1822000-09-11 04:00:46 +0000482 str(tb.tb_lineno)
483 ))
484 tb = tb.tb_next
485 if not tb:
486 break
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000487
Fred Drake526a1822000-09-11 04:00:46 +0000488 # just to be safe
489 del tb
Guido van Rossuma8d0f4f1999-06-08 13:20:05 +0000490
Fred Drake526a1822000-09-11 04:00:46 +0000491 file, function, line = tbinfo[-1]
Eric S. Raymondb49f4a42001-02-09 05:07:04 +0000492 info = '[' + '] ['.join(map(lambda x: '|'.join(x), tbinfo)) + ']'
Fred Drake526a1822000-09-11 04:00:46 +0000493 return (file, function, line), t, v, info
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000494
Andrew M. Kuchlingda85a272000-09-08 20:30:39 +0000495def close_all (map=None):
Fred Drake526a1822000-09-11 04:00:46 +0000496 if map is None:
497 map=socket_map
498 for x in map.values():
499 x.socket.close()
500 map.clear()
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000501
502# Asynchronous File I/O:
503#
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.
510#
511# What other OS's (besides NT) support async file i/o? [VMS?]
512#
513# Regardless, this is useful for pipes, and stdin/stdout...
514
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000515if os.name == 'posix':
Fred Drake526a1822000-09-11 04:00:46 +0000516 import fcntl
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000517
Fred Drake526a1822000-09-11 04:00:46 +0000518 class file_wrapper:
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):
522 self.fd = fd
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000523
Fred Drake526a1822000-09-11 04:00:46 +0000524 def recv (self, *args):
Jeremy Hyltonf32e4592002-04-04 21:02:24 +0000525 return os.read(self.fd, *args)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000526
Fred Drake526a1822000-09-11 04:00:46 +0000527 def send (self, *args):
Jeremy Hyltonf32e4592002-04-04 21:02:24 +0000528 return os.write(self.fd, *args)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000529
Fred Drake526a1822000-09-11 04:00:46 +0000530 read = recv
531 write = send
Andrew M. Kuchlingda85a272000-09-08 20:30:39 +0000532
Fred Drake526a1822000-09-11 04:00:46 +0000533 def close (self):
534 return os.close (self.fd)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000535
Fred Drake526a1822000-09-11 04:00:46 +0000536 def fileno (self):
537 return self.fd
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000538
Fred Drake526a1822000-09-11 04:00:46 +0000539 class file_dispatcher (dispatcher):
540 def __init__ (self, fd):
541 dispatcher.__init__ (self)
542 self.connected = 1
543 # set it to non-blocking mode
Fred Drakea94414a2001-05-10 15:33:31 +0000544 flags = fcntl.fcntl (fd, fcntl.F_GETFL, 0)
545 flags = flags | os.O_NONBLOCK
546 fcntl.fcntl (fd, fcntl.F_SETFL, flags)
Fred Drake526a1822000-09-11 04:00:46 +0000547 self.set_file (fd)
Guido van Rossum0039d7b1999-01-12 20:19:27 +0000548
Fred Drake526a1822000-09-11 04:00:46 +0000549 def set_file (self, fd):
550 self._fileno = fd
551 self.socket = file_wrapper (fd)
552 self.add_channel()