This module builds on the asyncore infrastructure, simplifying asynchronous clients and servers and making it easier to handle protocols whose elements are terminated by arbitrary strings, or are of variable length. asynchat defines the abstract class async_chat that you subclass, providing implementations of the collect_incoming_data() and found_terminator() methods. It uses the same asynchronous loop as asyncore, and the two types of channel, asyncore.dispatcher and asynchat.async_chat, can freely be mixed in the channel map. Typically an asyncore.dispatcher server channel generates new asynchat.async_chat channel objects as it receives incoming connection requests.
) |
Like asyncore.dispatcher, async_chat defines a set of events that are generated by an analysis of socket conditions after a select() call. Once the polling loop has been started the async_chat object's methods are called by the event-processing framework with no action on the part of the programmer.
Two class attributes can be modified, to improve performance, or possibly even to conserve memory.
4096
).
4096
).
Unlike asyncore.dispatcher, async_chat allows you to define a first-in-first-out queue (fifo) of producers. A producer need have only one method, more(), which should return data to be transmitted on the channel. The producer indicates exhaustion (i.e. that it contains no more data) by having its more() method return the empty string. At this point the async_chat object removes the producer from the fifo and starts using the next producer, if any. When the producer fifo is empty the handle_write() method does nothing. You use the channel object's set_terminator() method to describe how to recognize the end of, or an important breakpoint in, an incoming transmission from the remote endpoint.
To build a functioning async_chat subclass your input methods collect_incoming_data() and found_terminator() must handle the data that the channel receives asynchronously. The methods are described below.
) |
None
on to the producer fifo. When this producer is
popped off the fifo it causes the channel to be closed.
data) |
) |
) |
) |
) |
) |
) |
data) |
producer_fifo
to ensure its
transmission. This is all you need to do to have the channel write
the data out to the network, although it is possible to use your
own producers in more complex schemes to implement encryption and
chunking, for example.
producer) |
) |
True
for the channel to be included in the set of
channels tested by the select() loop for readability.
) |
None
then the channel
is closed.
term) |
term
may be any of three types of value, corresponding to three different ways
to handle incoming protocol data.
term | Description |
---|---|
string | Will call found_terminator() when the string is found in the input stream |
integer | Will call found_terminator() when the indicated number of characters have been received |
None |
The channel continues to collect data forever |
Note that any data following the terminator will be available for reading by the channel after found_terminator() is called.
) |
True
as long as items remain on the producer fifo,
or the channel is connected and the channel's output buffer is non-empty.