Implementation notes for socket timeouts and non-blocking I/O in jython
Fri Dec 26 20:59:46 GMT+00:00 2003
Implementing the asyncore module in jython
Sections
-
Introduction
-
Use the existing cpython asyncore module
-
Write a jython specific module, using jython
-
Write jython specific module, using java
[back to top]
Introduction
|
The purpose of this document is to discuss the implementation of the cpython asycnore module in jython.
The purpose of the asyncore module is to add the last piece of the puzzle in asynchronous processing: the dispatcher. A dispatcher is responsible for watching multiple registered channels. When a readiness event occurs on any channel, the dispatcher calls special events of a handler object associated with each channel.
Important points to note about the existing cpython asyncore module are as follows.
- It is written pure python.
- It uses only the interface defined by the cpython select module.
- It recreates both the ACCEPT and CONNECT events, even though the cpython select module "hides" these events as POLLIN and POLLOUT events.
There are three main strategies that can be adopted for implementing the asyncore module, along with pros and cons. These are listed in the sections below.
|
[back to top]
Use the existing cpython asyncore module
|
The simplest and quickest option for providing asyncore support in jython is to use the existing cpython module. The advantages of this approach include
- No new code need be written
- Existing code is widely used, and thus well tested.
The disadvantages are
- Potential for error because of the possibility of different semantics of ACCEPT and CONNECT events on cpython vs. java (on multiple platforms).
- Mildly inefficient, because of all the mask translation and dictionary lookups
- Existing code is messy because of varying platform support for the select.select() and select.poll() objects in cpython.
|
[back to top]
Write a jython specific module, using jython
|
Another approach is write a jython specific asyncore module, written in jython, but directly using the java.nio APIs. There is quite a close correlation between the models in the cpython asyncore module and java.nio.channels.Selector objects. It is possible to write a more efficient asyncore implemenation by using the java APIs directly. The advantanges of this approach are
- Can be more robust because of java's explicit support for ACCEPT and CONNECT events, which can be directly translated to the relevant "dispatcher" events.
- Can be slightly more efficient, because it avoids a layer of calls to jython implemented poll objects.
- Asyncore model maps directly onto semantics of a java.nio.channels.Selector (even more than cpython)
- "Dispatcher" objects can be registered as attached objects to the java.nio.channels.SelectionKeys that represent them. This would make them directly available to the asyncore.loop() function (which would receive a set of java.nio.channels.SelectionKeys, each having the dispatcher as an attachment, which avoids the dictionary lookups involved in poll objects.
- Can easily be translated to java later on.
The disadvantages are
- New code needs to be written. Not too much though.
- Extensive testing will have to be done to ensure that the new module implements the precise contract of the cpython asyncore module.
TBD: Must write sample code for a jython asyncore module.
|
[back to top]
Write jython specific module, using java
|
The last major approach is to, as above, write a jython specific module, but in java. However, this is unlikely to the optimal approach, at least in the short term. Advantages include
- Best possible efficiency
The disadvantages are
- A lot of hard work!
- The java/jython object model is likely to change soon.
|
|