pybitmessage.network.asyncore_pollchoose module

src/network/asyncore_pollchoose.py

# ====================================================================== # Copyright 1996 by Sam Rushing # # All Rights Reserved # # Permission to use, copy, modify, and distribute this software and # its documentation for any purpose and without fee is hereby # granted, provided that the above copyright notice appear in all # copies and that both that copyright notice and this permission # notice appear in supporting documentation, and that the name of Sam # Rushing not be used in advertising or publicity pertaining to # distribution of the software without specific, written prior # permission. # # SAM RUSHING DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN # NO EVENT SHALL SAM RUSHING BE LIABLE FOR ANY SPECIAL, INDIRECT OR # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS # OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # ======================================================================

Basic infrastructure for asynchronous socket service clients and servers.

There are only two ways to have a program on a single processor do “more than one thing at a time”. Multi-threaded programming is the simplest and most popular way to do it, but there is another very different technique, that lets you have nearly all the advantages of multi-threading, without actually using multiple threads. it’s really only practical if your program is largely I/O bound. If your program is CPU bound, then pre-emptive scheduled threads are probably what you really need. Network servers are rarely CPU-bound, however.

If your operating system supports the select() system call in its I/O library (and nearly all do), then you can use it to juggle multiple communication channels at once; doing other work while your I/O is taking place in the “background.” Although this strategy can seem strange and complex, especially at first, it is in many ways easier to understand and control than multi-threaded programming. The module documented here solves many of the difficult problems for you, making the task of building sophisticated high-performance network servers and clients a snap.

exception ExitNow[source]

Bases: exceptions.Exception

We don’t use directly but may be necessary as we replace asyncore due to some library raising or expecting it

can_receive()[source]

Predicate indicating whether the download throttle is in effect

can_send()[source]

Predicate indicating whether the upload throttle is in effect

close_all(map=None, ignore_all=False)[source]

Close all connections

compact_traceback()[source]

Return a compact traceback

class dispatcher(sock=None, map=None)[source]

Dispatcher for socket objects

accept()

Accept incoming connections. Returns either an address pair or None.

accepting = False
add_channel(map=None)

Add a channel

addr = None
bind(addr)

Bind to an address

close()

Close connection

closing = False
connect(address)

Connect to an address

connected = False
connecting = False
create_socket(family=2, socket_type=1)

Create a socket

debug = False
del_channel(map=None)

Delete a channel

handle_accept()

Handle an accept event

handle_accepted(sock, addr)

Log that the subclass does not implement handle_accepted

handle_close()

Log that the subclass does not implement handle_close

handle_connect()

Log that the subclass does not implement handle_connect

handle_connect_event()

Handle a connection event

handle_error()

Handle unexpected exceptions

handle_expt()

Log that the subclass does not implement handle_expt

handle_expt_event()

Handle expected exceptions

handle_read()

Log that the subclass does not implement handle_read

handle_read_event()

Handle a read event

handle_write()

Log that the subclass does not implement handle_write

handle_write_event()

Handle a write event

ignore_log_types = frozenset(['warning'])
listen(num)

Listen on a port

log(message)

Log a message to stderr

log_info(message, log_type='info')

Conditionally print a message

minTx = 1500
poller_flags = 0
poller_registered = False
readable()

Predicate to indicate download throttle status

recv(buffer_size)

Receive data

send(data)

Send data

set_reuse_addr()

try to re-use a server port if possible

set_socket(sock, map=None)

Set socket

writable()

Predicate to indicate upload throttle status

class dispatcher_with_send(sock=None, map=None)[source]

Bases: pybitmessage.network.asyncore_pollchoose.dispatcher

adds simple buffered output capability, useful for simple clients. [for more sophisticated usage use asynchat.async_chat]

handle_write()

Handle a write event

initiate_send()

Initiate a send

send(data)

Send data

writable()

Predicate to indicate if the object is writable

epoll_poller(timeout=0.0, map=None)[source]

A poller which uses epoll(), supported on Linux 2.5.44 and newer.

class file_dispatcher(fd, map=None)[source]

Bases: pybitmessage.network.asyncore_pollchoose.dispatcher

A dispatcher for file_wrapper objects

set_file(fd)

Set file

class file_wrapper(fd)[source]

Here we override just enough to make a file look like a socket for the purposes of asyncore.

The passed fd is automatically os.dup()’d

close()

Fake close()

fileno()

Fake fileno()

getsockopt(level, optname, buflen=None)

Fake getsockopt()

read(*args)

Fake recv()

recv(*args)

Fake recv()

send(*args)

Fake send()

write(*args)

Fake send()

kqueue_poller(timeout=0.0, map=None)[source]

A poller which uses kqueue(), BSD specific.

loop(timeout=30.0, use_poll=False, map=None, count=None, poller=None)[source]

Poll in a loop, until count or timeout is reached

poll(timeout=0.0, map=None)[source]

A poller which uses select(), available on most platforms.

poll2(timeout=0.0, map=None)[source]

A poller which uses poll(), available on most UNIXen.

poll3(timeout=0.0, map=None)[source]

A poller which uses poll(), available on most UNIXen.

poll_poller(timeout=0.0, map=None)[source]

A poller which uses poll(), available on most UNIXen.

read(obj)[source]

Event to read from the object, i.e. its network socket.

readwrite(obj, flags)[source]

Read and write any pending data to/from the object

select_poller(timeout=0.0, map=None)[source]

A poller which uses select(), available on most platforms.

set_rates(download, upload)[source]

Set throttling rates

update_received(download=0)[source]

Update the receiving throttle

update_sent(upload=0)[source]

Update the sending throttle

write(obj)[source]

Event to write to the object, i.e. its network socket.