Welcome, guest | Sign In | My Account | Store | Cart

Asynchronous TCP socket service

Python, 628 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
import collections
import errno
import functools
import select
import socket

'''Asynchronous socket service inspired by the basic design of Boost ASIO.

This service currently supports TCP sockets only, and supports asynchronous
versions of common client operations (connect, read, write) and server operations 
(accept).

This implementation supports the use of select, poll, epoll, or kqueue as the
underlying poll system call.

Aaron Riekenberg
aaron.riekenberg@gmail.com
'''

class AsyncException(Exception):

  def __init__(self, value):
    super(AsyncException, self).__init__()
    self.__value = value

  def __str__(self):
    return repr(self.__value)

class AsyncSocket(object):

  '''Socket class supporting asynchronous operations.'''

  def __init__(self, asyncIOService, sock = None):
    super(AsyncSocket, self).__init__()
    self.__asyncIOService = asyncIOService
    self.__acceptCallback = None
    self.__connectCallback = None
    self.__readCallback = None
    self.__writeAllCallback = None
    self.__writeBuffer = b''
    self.__maxReadBytes = 0
    self.__closed = False
    if sock:
      self.__socket = sock
    else:
      self.__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.__socket.setblocking(0)
    asyncIOService.addAsyncSocket(self)

  def __str__(self):
    return ('AsyncSocket [ fileno = %d ]' % self.fileno())

  def getsockname(self):
    return self.__socket.getsockname()

  def getpeername(self):
    return self.__socket.getpeername()
 
  def closed(self):
    return self.__closed

  def getSocket(self):
    return self.__socket

  def fileno(self):
    return self.__socket.fileno()

  def setReuseAddress(self):
    self.__socket.setsockopt(
      socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

  def listen(self, backlog = socket.SOMAXCONN):
    self.__socket.listen(backlog)

  def bind(self, addr):
    self.__socket.bind(addr)

  def asyncConnect(self, address, callback):
    if (self.__acceptCallback):
      raise AsyncException('Accept already in progress')
    if (self.__connectCallback):
      raise AsyncException('Connect already in progress')
    if (self.__readCallback):
      raise AsyncException('Read already in progress')
    if (self.__writeAllCallback):
      raise AsyncException('Write all already in progress')
    if (self.__closed):
      raise AsyncException('AsyncSocket closed')

    err = self.__socket.connect_ex(address)
    if err in (errno.EINPROGRESS, errno.EWOULDBLOCK):
      self.__connectCallback = callback
      self.__asyncIOService.registerAsyncSocketForWrite(self)
    else:
      self.__asyncIOService.invokeLater(
        functools.partial(callback, err = err))

  def asyncAccept(self, callback):
    if (self.__acceptCallback):
      raise AsyncException('Accept already in progress')
    if (self.__connectCallback):
      raise AsyncException('Connect already in progress')
    if (self.__readCallback):
      raise AsyncException('Read already in progress')
    if (self.__writeAllCallback):
      raise AsyncException('Write all already in progress')
    if (self.__closed):
      raise AsyncException('AsyncSocket closed')

    try:
      (newSocket, addr) = self.__socket.accept()
      asyncSocket = AsyncSocket(self.__asyncIOService, newSocket)
      self.__asyncIOService.invokeLater(
        functools.partial(callback, sock = asyncSocket, err = 0))
    except socket.error as e:
      if e.args[0] in (errno.EAGAIN, errno.EWOULDBLOCK):
        self.__acceptCallback = callback
        self.__asyncIOService.registerAsyncSocketForRead(self)
      else:
        self.__asyncIOService.invokeLater(
          functools.partial(callback, sock = None, err = e.args[0]))

  def asyncRead(self, maxBytes, callback):
    if (self.__acceptCallback):
      raise AsyncException('Accept already in progress')
    if (self.__connectCallback):
      raise AsyncException('Connect already in progress')
    if (self.__readCallback):
      raise AsyncException('Read already in progress')
    if (self.__closed):
      raise AsyncException('AsyncSocket closed')

    self.__maxReadBytes = maxBytes
    try:
      data = self.__socket.recv(self.__maxReadBytes)
      self.__asyncIOService.invokeLater(
        functools.partial(callback, data = data, err = 0))
    except socket.error as e:
      if e.args[0] in (errno.EAGAIN, errno.EWOULDBLOCK):
        self.__readCallback = callback
        self.__asyncIOService.registerAsyncSocketForRead(self)
      else:
        self.__asyncIOService.invokeLater(
          functools.partial(callback, data = data, err = e.args[0]))

  def asyncWriteAll(self, data, callback):
    if (self.__acceptCallback):
      raise AsyncException('Accept already in progress')
    if (self.__connectCallback):
      raise AsyncException('Connect already in progress')
    if (self.__writeAllCallback):
      raise AsyncException('Write all already in progress')
    if (self.__closed):
      raise AsyncException('AsyncSocket closed')

    self.__writeBuffer += data
    writeWouldBlock = False
    try:
      bytesSent = self.__socket.send(self.__writeBuffer)
      self.__writeBuffer = self.__writeBuffer[bytesSent:]
      if (len(self.__writeBuffer) == 0):
        self.__asyncIOService.invokeLater(
          functools.partial(callback, err = 0))
      else:
        writeWouldBlock = True
    except socket.error as e:
      if e.args[0] in (errno.EAGAIN, errno.EWOULDBLOCK):
        writeWouldBlock = True
      else:
        self.__asyncIOService.invokeLater(
          functools.partial(callback, err = e.args[0]))

    if (writeWouldBlock):
      self.__writeAllCallback = callback
      self.__asyncIOService.registerAsyncSocketForWrite(self)

  def close(self):
    if self.__closed:
      return

    self.__asyncIOService.removeAsyncSocket(self)
    self.__socket.close()
    self.__closed = True

    if self.__acceptCallback:
      self.__asyncIOService.invokeLater(
        functools.partial(self.__acceptCallback, sock = None, err = errno.EBADF))
      self.__acceptCallback = None

    if self.__connectCallback:
      self.__asyncIOService.invokeLater(
        functools.partial(self.__connectCallback, err = errno.EBADF))
      self.__connectCallback = None

    if self.__readCallback:
      self.__asyncIOService.invokeLater(
        functools.partial(self.__readCallback, data = None, err = errno.EBADF))
      self.__readCallback = None

    if self.__writeAllCallback:
      self.__asyncIOService.invokeLater(
        functools.partial(self.__writeAllCallback, err = errno.EBADF))
      self.__writeAllCallback = None

  def handleError(self):
    err = self.__socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)

    if (self.__connectCallback):
      self.__asyncIOService.unregisterAsyncSocketForWrite(self)
      self.__asyncIOService.invokeLater(
        functools.partial(self.__connectCallback, err = err))
      self.__connectCallback = None

    if (self.__acceptCallback):
      self.__asyncIOService.unregisterAsyncSocketForRead(self)
      self.__asyncIOService.invokeLater(
        functools.partial(self.__acceptCallback, sock = None, err = err))
      self.__acceptCallback = None

    if (self.__readCallback):
      self.__asyncIOService.unregisterAsyncSocketForRead(self)
      self.__asyncIOService.invokeLater(
        functools.partial(self.__readCallback, data = None, err = err))
      self.__readCallback = None

    if (self.__writeAllCallback):
      self.__asyncIOService.unregisterAsyncSocketForWrite(self)
      self.__asyncIOService.invokeLater(
        functools.partial(self.__writeAllCallback, err = err))
      self.__writeAllCallback = None

  def handleRead(self):
    if (self.__acceptCallback):
      try:
        (newSocket, addr) = self.__socket.accept()
        asyncSocket = AsyncSocket(self.__asyncIOService, newSocket)
        self.__asyncIOService.unregisterAsyncSocketForRead(self)
        self.__asyncIOService.invokeLater(
          functools.partial(self.__acceptCallback, sock = asyncSocket, err = 0))
        self.__acceptCallback = None
      except socket.error as e:
        if e.args[0] in (errno.EAGAIN, errno.EWOULDBLOCK):
          pass
        else:
          self.__asyncIOService.unregisterAsyncSocketForRead(self)
          self.__asyncIOService.invokeLater(
            functools.partial(self.__acceptCallback, sock = None, err = e.args[0]))
          self.__acceptCallback = None

    if (self.__readCallback):
      try:
        data = self.__socket.recv(self.__maxReadBytes)
        self.__asyncIOService.unregisterAsyncSocketForRead(self)
        self.__asyncIOService.invokeLater(
          functools.partial(self.__readCallback, data = data, err = 0))
        self.__readCallback = None
      except socket.error as e:
        if e.args[0] in (errno.EAGAIN, errno.EWOULDBLOCK):
          pass
        else:
          self.__asyncIOService.unregisterAsyncSocketForRead(self)
          self.__asyncIOService.invokeLater(
            functools.partial(self.__readCallback, data = None, err = e.args[0]))
          self.__readCallback = None

  def handleWrite(self):
    if (self.__connectCallback):
      err = self.__socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
      if err not in (errno.EINPROGRESS, errno.EWOULDBLOCK):
        self.__asyncIOService.unregisterAsyncSocketForWrite(self)
        self.__asyncIOService.invokeLater(
          functools.partial(self.__connectCallback, err = err))
        self.__connectCallback = None

    if (self.__writeAllCallback):
      try:
        bytesSent = self.__socket.send(self.__writeBuffer)
        self.__writeBuffer = self.__writeBuffer[bytesSent:]
        if (len(self.__writeBuffer) == 0):
          self.__asyncIOService.unregisterAsyncSocketForWrite(self)
          self.__asyncIOService.invokeLater(
            functools.partial(self.__writeAllCallback, err = 0))
          self.__writeAllCallback = None
      except socket.error as e:
        if e.args[0] in (errno.EAGAIN, errno.EWOULDBLOCK):
          pass
        else:
          self.__asyncIOService.unregisterAsyncSocketForWrite(self)
          self.__asyncIOService.invokeLater(
            functools.partial(self.__writeAllCallback, err = e.args[0]))
          self.__writeAllCallback = None

class AsyncIOService(object):

  '''Service used to poll asynchronous sockets.'''

  def __init__(self):
    self.__fdToAsyncSocket = {}
    self.__fdsRegisteredForRead = set()
    self.__fdsRegisteredForWrite = set()
    self.__eventQueue = collections.deque()

  def createAsyncSocket(self):
    return AsyncSocket(asyncIOService = self)

  def addAsyncSocket(self, asyncSocket):
    self.__fdToAsyncSocket[asyncSocket.fileno()] = asyncSocket

  def removeAsyncSocket(self, asyncSocket):
    fileno = asyncSocket.fileno()
    if fileno in self.__fdToAsyncSocket:
      del self.__fdToAsyncSocket[fileno]
    if ((fileno in self.__fdsRegisteredForRead) or
        (fileno in self.__fdsRegisteredForWrite)):
      self.unregisterForEvents(asyncSocket)
      self.__fdsRegisteredForRead.discard(fileno)
      self.__fdsRegisteredForWrite.discard(fileno)

  def invokeLater(self, event):
    self.__eventQueue.append(event)

  def registerAsyncSocketForRead(self, asyncSocket):
    fileno = asyncSocket.fileno()
    if fileno not in self.__fdsRegisteredForRead:
      if fileno in self.__fdsRegisteredForWrite:
        self.modifyRegistrationForEvents(asyncSocket, readEvents = True, writeEvents = True)
      else:
        self.registerForEvents(asyncSocket, readEvents = True, writeEvents = False)
      self.__fdsRegisteredForRead.add(fileno)

  def unregisterAsyncSocketForRead(self, asyncSocket):
    fileno = asyncSocket.fileno()
    if fileno in self.__fdsRegisteredForRead:
      if fileno in self.__fdsRegisteredForWrite:
        self.modifyRegistrationForEvents(asyncSocket, readEvents = False, writeEvents = True)
      else:
        self.unregisterForEvents(asyncSocket)
      self.__fdsRegisteredForRead.discard(fileno)

  def registerAsyncSocketForWrite(self, asyncSocket):
    fileno = asyncSocket.fileno()
    if fileno not in self.__fdsRegisteredForWrite:
      if fileno in self.__fdsRegisteredForRead:
        self.modifyRegistrationForEvents(asyncSocket, readEvents = True, writeEvents = True)
      else:
        self.registerForEvents(asyncSocket, readEvents = False, writeEvents = True)
      self.__fdsRegisteredForWrite.add(fileno)

  def unregisterAsyncSocketForWrite(self, asyncSocket):
    fileno = asyncSocket.fileno()
    if fileno in self.__fdsRegisteredForWrite:
      if fileno in self.__fdsRegisteredForRead:
        self.modifyRegistrationForEvents(asyncSocket, readEvents = True, writeEvents = False)
      else:
        self.unregisterForEvents(asyncSocket)
      self.__fdsRegisteredForWrite.discard(fileno)

  def getReadFDSet(self):
    return self.__fdsRegisteredForRead

  def getWriteFDSet(self):
    return self.__fdsRegisteredForWrite

  def getNumFDs(self):
    return len(self.__fdToAsyncSocket)

  def registerForEvents(self, asyncSocket, readEvents, writeEvents):
    raise NotImplementedError

  def modifyRegistrationForEvents(self, asyncSocket, readEvents, writeEvents):
    raise NotImplementedError

  def unregisterForEvents(self, asyncSocket):
    raise NotImplementedError

  def doPoll(self, block):
    raise NotImplementedError

  def run(self):
    while True:
      # As we process events in self.__eventQueue, more events are likely
      # to be added to it by invokeLater.  We don't want to starve events
      # coming in from doPoll, so we limit the number of events processed
      # from self.__eventQueue to the initial size of the queue.  After this if
      # the queue is still not empty, set doPoll to be non blocking so we get
      # back to processing events in the queue in a timely manner.
      initialQueueLength = len(self.__eventQueue)
      eventsProcessed = 0
      while ((len(self.__eventQueue) > 0) and
             (eventsProcessed < initialQueueLength)):
        event = self.__eventQueue.popleft()
        event()
        eventsProcessed += 1

      if ((len(self.__eventQueue) == 0) and
          (len(self.__fdsRegisteredForRead) == 0) and
          (len(self.__fdsRegisteredForWrite) == 0)):
        break

      block = True
      if (len(self.__eventQueue) > 0):
        block = False
      self.doPoll(block = block)

  def handleEventForFD(self, fd, readReady, writeReady, errorReady):
    if fd in self.__fdToAsyncSocket:
      asyncSocket = self.__fdToAsyncSocket[fd]
      if (readReady):
        asyncSocket.handleRead()
      if (writeReady):
        asyncSocket.handleWrite()
      if (errorReady):
        asyncSocket.handleError()

class EPollAsyncIOService(AsyncIOService):

  def __init__(self):
    super(EPollAsyncIOService, self).__init__()
    self.__poller = select.epoll()

  def __str__(self):
    return ('EPollAsyncIOService [ fileno = %d ]' % self.__poller.fileno())

  def registerForEvents(self, asyncSocket, readEvents, writeEvents):
    fileno = asyncSocket.fileno()
    eventMask = 0
    if (readEvents):
      eventMask |= select.EPOLLIN
    if (writeEvents):
      eventMask |= select.EPOLLOUT
    self.__poller.register(fileno, eventMask)

  def modifyRegistrationForEvents(self, asyncSocket, readEvents, writeEvents):
    fileno = asyncSocket.fileno()
    eventMask = 0
    if (readEvents):
      eventMask |= select.EPOLLIN
    if (writeEvents):
      eventMask |= select.EPOLLOUT
    self.__poller.modify(fileno, eventMask)

  def unregisterForEvents(self, asyncSocket):
    fileno = asyncSocket.fileno()
    self.__poller.unregister(fileno)

  def doPoll(self, block):
    readyList = self.__poller.poll(-1 if block else 0)
    for (fd, eventMask) in readyList:
      readReady = ((eventMask & select.EPOLLIN) != 0)
      writeReady = ((eventMask & select.EPOLLOUT) != 0)
      errorReady = ((eventMask & 
                     (select.EPOLLERR | select.EPOLLHUP)) != 0)
      self.handleEventForFD(fd = fd,
                            readReady = readReady,
                            writeReady = writeReady,
                            errorReady = errorReady)

class KQueueAsyncIOService(AsyncIOService):

  def __init__(self):
    super(KQueueAsyncIOService, self).__init__()
    self.__kqueue = select.kqueue()

  def __str__(self):
    return ('KQueueAsyncIOService [ fileno = %d ]' % self.__kqueue.fileno())

  def registerForEvents(self, asyncSocket, readEvents, writeEvents):
    fileno = asyncSocket.fileno()
    if readEvents:
      readKE = select.kevent(ident = fileno,
                             filter = select.KQ_FILTER_READ,
                             flags = select.KQ_EV_ADD)
    else:
      readKE = select.kevent(ident = fileno,
                             filter = select.KQ_FILTER_READ,
                             flags = (select.KQ_EV_ADD | select.KQ_EV_DISABLE))
    if writeEvents:
      writeKE = select.kevent(ident = fileno,
                              filter = select.KQ_FILTER_WRITE,
                              flags = select.KQ_EV_ADD)
    else:
      writeKE = select.kevent(ident = fileno,
                              filter = select.KQ_FILTER_WRITE,
                              flags = (select.KQ_EV_ADD | select.KQ_EV_DISABLE))
    # Should be able to put readKE and writeKE in a list in
    # one call to kqueue.control, but this is broken due to Python issue 5910
    self.__kqueue.control([readKE], 0, 0)
    self.__kqueue.control([writeKE], 0, 0)

  def modifyRegistrationForEvents(self, asyncSocket, readEvents, writeEvents):
    fileno = asyncSocket.fileno()
    if readEvents:
      readKE = select.kevent(ident = fileno,
                             filter = select.KQ_FILTER_READ,
                             flags = select.KQ_EV_ENABLE)
    else:
      readKE = select.kevent(ident = fileno,
                             filter = select.KQ_FILTER_READ,
                             flags = select.KQ_EV_DISABLE)
    if writeEvents:
      writeKE = select.kevent(ident = fileno,
                              filter = select.KQ_FILTER_WRITE,
                              flags = select.KQ_EV_ENABLE)
    else:
      writeKE = select.kevent(ident = fileno,
                              filter = select.KQ_FILTER_WRITE,
                              flags = select.KQ_EV_DISABLE)
    # Should be able to put readKE and writeKE in a list in
    # one call to kqueue.control, but this is broken due to Python issue 5910
    self.__kqueue.control([readKE], 0, 0)
    self.__kqueue.control([writeKE], 0, 0)

  def unregisterForEvents(self, asyncSocket):
    fileno = asyncSocket.fileno()
    readKE = select.kevent(ident = fileno,
                           filter = select.KQ_FILTER_READ,
                           flags = select.KQ_EV_DELETE)
    writeKE = select.kevent(ident = fileno,
                            filter = select.KQ_FILTER_WRITE,
                            flags = select.KQ_EV_DELETE)
    # Should be able to put readKE and writeKE in a list in
    # one call to kqueue.control, but this is broken due to Python issue 5910
    self.__kqueue.control([readKE], 0, 0)
    self.__kqueue.control([writeKE], 0, 0)

  def doPoll(self, block):
    eventList = self.__kqueue.control(
                  None,
                  self.getNumFDs() * 2,
                  None if block else 0)
    for ke in eventList:
      fd = ke.ident
      readReady = (ke.filter == select.KQ_FILTER_READ)
      writeReady = (ke.filter == select.KQ_FILTER_WRITE)
      errorReady = ((ke.flags & select.KQ_EV_EOF) != 0)
      self.handleEventForFD(fd = fd,
                            readReady = readReady,
                            writeReady = writeReady,
                            errorReady = errorReady)

class PollAsyncIOService(AsyncIOService):

  def __init__(self):
    super(PollAsyncIOService, self).__init__()
    self.__poller = select.poll()

  def __str__(self):
    return 'PollAsyncIOService'

  def registerForEvents(self, asyncSocket, readEvents, writeEvents):
    fileno = asyncSocket.fileno()
    eventMask = 0
    if (readEvents):
      eventMask |= select.POLLIN
    if (writeEvents):
      eventMask |= select.POLLOUT
    self.__poller.register(fileno, eventMask)

  def modifyRegistrationForEvents(self, asyncSocket, readEvents, writeEvents):
    fileno = asyncSocket.fileno()
    eventMask = 0
    if (readEvents):
      eventMask |= select.POLLIN
    if (writeEvents):
      eventMask |= select.POLLOUT
    self.__poller.modify(fileno, eventMask)

  def unregisterForEvents(self, asyncSocket):
    fileno = asyncSocket.fileno()
    self.__poller.unregister(fileno)

  def doPoll(self, block):
    readyList = self.__poller.poll(None if block else 0)
    for (fd, eventMask) in readyList:
      readReady = ((eventMask & select.POLLIN) != 0)
      writeReady = ((eventMask & select.POLLOUT) != 0)
      errorReady = ((eventMask & 
                     (select.POLLERR | select.POLLHUP | select.POLLNVAL)) != 0)
      self.handleEventForFD(fd = fd,
                            readReady = readReady,
                            writeReady = writeReady,
                            errorReady = errorReady)

class SelectAsyncIOService(AsyncIOService):

  def __init__(self):
    super(SelectAsyncIOService, self).__init__()

  def __str__(self):
    return 'SelectAsyncIOService'

  def registerForEvents(self, asyncSocket, readEvents, writeEvents):
    pass

  def modifyRegistrationForEvents(self, asyncSocket, readEvents, writeEvents):
    pass

  def unregisterForEvents(self, asyncSocket):
    pass

  def doPoll(self, block):
    allFDSet = self.getReadFDSet() | self.getWriteFDSet()
    (readList, writeList, exceptList) = \
      select.select(self.getReadFDSet(), self.getWriteFDSet(), allFDSet,
                    None if block else 0)
    for fd in allFDSet:
      readReady = fd in readList
      writeReady = fd in writeList
      errorReady = fd in exceptList
      if (readReady or writeReady or errorReady):
        self.handleEventForFD(fd = fd,
                              readReady = readReady,
                              writeReady = writeReady,
                              errorReady = errorReady)

def createAsyncIOService(allow_epoll = True,
                         allow_kqueue = True,
                         allow_poll = True):
  '''Create an AsyncIOService supported by the platform and parameters.'''

  if (allow_epoll and hasattr(select, 'epoll')):
    return EPollAsyncIOService()
  elif (allow_kqueue and hasattr(select, 'kqueue')):
    return KQueueAsyncIOService()
  elif (allow_poll and hasattr(select, 'poll')):
    return PollAsyncIOService()
  else:
    return SelectAsyncIOService()

This is an asynchronous socket service inspired by the excellent boost::asio library for c++.

Currently this implementation supports TCP sockets only. Asynchronous read, write, connect, and accept operations are supported. Each asynchronous operation takes a callback function which is invoked when the operation completes. Unlike boost::asio this module is not thread safe - AsyncIOService.run may only be called from one thread.

The AsyncIOService class is designed to be generic and support any underlying poll implementation. Currently there are subclasses of AsyncIOService that use epoll, kqueue, poll, and select. The createAsyncIOService function automatically selects the best implementation available.

An obvious alternative to this implementation is the asyncore module in the standard library. However in my opinion there are at least a couple of advantages to this asio module:

In asyncore asynchronous operations are always in progress for any open sockets. This can only be controlled by overriding the readable() and writable() methods. With boost::asio and this asio module, asynchronous operations are only in progress when the user explicitly starts them by calling asyncRead, asyncWrite, etc. In my mind this is easier to reason about.

Additionally with asyncore the handle_read and handle_write methods just notify the user's code that the socket is readable or writable - the user is then responsible for actually doing the reading or writing. With asio the data to write is simply passed to asyncWriteAll, and the callback from asyncRead includes data that was read.

3 comments

See http://code.activestate.com/recipes/577663-asio-proxy/ for an example of how to use this module (small TCP proxy application).

James Mills 13 years ago  # | flag

Have you not heard of Twisted ? (Or my own work circuits) ?

Andreas Balogh 13 years ago  # | flag

IMHO to produce even more compact and readable code without callbacks you should give coroutine based packages like eventlet or gevent a try.