txaio.aio

Attributes

Classes

AsyncGeneratorType

FailedFuture

This provides an object with any features from Twisted's Failure

_AsyncioApi

_TxaioFileHandler

Handler instances dispatch logging events to specific destinations.

_TxaioLogWrapper

This defines the methods you can call on the object returned from

Functions

_log(logger, level[, format])

_no_op(*args, **kw)

add_log_categories(categories)

get_global_log_level()

make_logger()

set_global_log_level(level)

Set the global log level on all loggers instantiated by txaio.

start_logging([out, level])

Begin logging.

with_config([loop])

Module Contents

class AsyncGeneratorType[source]
class FailedFuture(type_, value, traceback)[source]

Bases: txaio.interfaces.IFailedFuture

This provides an object with any features from Twisted’s Failure that we might need in Autobahn classes that use FutureMixin.

We need to encapsulate information from exceptions so that errbacks still have access to the traceback (in case they want to print it out) outside of “except” blocks.

__str__()[source]
_traceback[source]
_type[source]
_value[source]
property value[source]

An actual Exception instance. Same as the second item returned from sys.exc_info()

class _AsyncioApi(config)[source]

Bases: object

_config[source]
property _loop[source]
add_callbacks(future, callback, errback)[source]

callback or errback may be None, but at least one must be non-None.

as_future(fun, *args, **kwargs)[source]
call_later(delay, fun, *args, **kwargs)[source]
cancel(future, msg=None)[source]
create_failure(exception=None)[source]

This returns an object implementing IFailedFuture.

If exception is None (the default) we MUST be called within an “except” block (such that sys.exc_info() returns useful information).

create_future(result=_unspecified, error=_unspecified, canceller=_unspecified)[source]
create_future_error(error=None)[source]
create_future_success(result)[source]
failure_format_traceback(fail)[source]
Parameters:

fail – must be an IFailedFuture

returns a string

failure_message(fail)[source]
Parameters:

fail – must be an IFailedFuture

returns a unicode error-message

failure_traceback(fail)[source]
Parameters:

fail – must be an IFailedFuture

returns a traceback instance

gather(futures, consume_exceptions=True)[source]

This returns a Future that waits for all the Futures in the list futures

Parameters:
  • futures – a list of Futures (or coroutines?)

  • consume_exceptions – if True, any errors are eaten and

returned in the result list.

is_called(future)[source]
is_future(obj)[source]
make_batched_timer(bucket_seconds, chunk_size=100)[source]

Creates and returns an object implementing txaio.IBatchedTimer.

Parameters:
  • bucket_seconds – the number of seconds in each bucket. That is, a value of 5 means that any timeout within a 5 second window will be in the same bucket, and get notified at the same time. This is only accurate to “milliseconds”.

  • chunk_size – when “doing” the callbacks in a particular bucket, this controls how many we do at once before yielding to the reactor.

reject(future, error=None)[source]
resolve(future, result=None)[source]
sleep(delay)[source]

Inline sleep for use in co-routines.

Parameters:

delay (float) – Time to sleep in seconds.

using_asyncio = True[source]
using_twisted = False[source]
class _TxaioFileHandler(fileobj, **kw)[source]

Bases: logging.Handler, object

Handler instances dispatch logging events to specific destinations.

The base handler class. Acts as a placeholder which defines the Handler interface. Handlers can optionally use Formatter instances to format records as desired. By default, no formatter is specified; in this case, the ‘raw’ message as determined by record.message is logged.

_encode = True[source]
_file[source]
emit(record)[source]

Do whatever it takes to actually log the specified logging record.

This version is intended to be implemented by subclasses and so raises a NotImplementedError.

class _TxaioLogWrapper(logger)[source]

Bases: txaio.interfaces.ILogger

This defines the methods you can call on the object returned from txaio.make_logger() – although the actual object may have additional methods, you should only call the methods listed here.

All the log methods have the same signature, they just differ in what “log level” they represent to the handlers/emitters. The message argument is a format string using PEP3101-style references to things from the kwargs. Note that there are also the following keys added to the kwargs: log_time and log_level.

For example:

class MyThing(object):
    log = txaio.make_logger()

    def something_interesting(self, things=dict(one=1, two=2)):
        try:
            self.log.debug("Called with {things[one]}", things=things)
            result = self._method_call()
            self.log.info("Got '{result}'.", result=result)
        except Exception:
            fail = txaio.create_failure()
            self.log.critical(txaio.failure_format_traceback(fail))

The philsophy behind txaio’s interface is fairly similar to Twisted’s logging APIs after version 15. See Twisted’s documentation for details.

_logger[source]
_set_log_level(level)[source]
emit(level, *args, **kwargs)[source]
_categories[source]
_default_api[source]
_log(logger, level, format='', **kwargs)[source]
_log_level = 'info'[source]
_loggers[source]
_no_op(*args, **kw)[source]
_started_logging = False[source]
_unspecified[source]
add_callbacks[source]
add_log_categories(categories)[source]
as_future[source]
call_later[source]
cancel[source]
config[source]
create_failure[source]
create_future[source]
create_future_error[source]
create_future_success[source]
failure_format_traceback[source]
failure_message[source]
failure_traceback[source]
gather[source]
get_global_log_level()[source]
is_called[source]
is_future[source]
make_batched_timer[source]
make_logger()[source]
perf_counter_ns[source]
reject[source]
resolve[source]
set_global_log_level(level)[source]

Set the global log level on all loggers instantiated by txaio.

sleep[source]
sleep[source]
start_logging(out=_stdout, level='info')[source]

Begin logging.

Parameters:
  • out – if provided, a file-like object to log to. By default, this is stdout.

  • level – the maximum log-level to emit (a string)

time_ns[source]
using_asyncio = True[source]
using_twisted = False[source]
with_config(loop=None)[source]
Returns:

an instance of the txaio API with the given configuration. This won’t affect anything using the ‘gloabl’ config nor other instances created using this function.

If you need to customize txaio configuration separately (e.g. to use multiple event-loops in asyncio), you can take code like this:

import txaio

class FunTimes(object):

def something_async(self):

return txaio.call_later(1, lambda: ‘some result’)

and instead do this:

import txaio

class FunTimes(object):

txaio = txaio

def something_async(self):

# this will run in the local/new event loop created in the constructor return self.txaio.call_later(1, lambda: ‘some result’)

fun0 = FunTimes() fun1 = FunTimes() fun1.txaio = txaio.with_config(loop=asyncio.new_event_loop())

So fun1 will run its futures on the newly-created event loop, while fun0 will work just as it did before this with_config method was introduced (after 2.6.2).