txaio.aio¶
Attributes¶
Classes¶
This provides an object with any features from Twisted's Failure |
|
Handler instances dispatch logging events to specific destinations. |
|
This defines the methods you can call on the object returned from |
Functions¶
|
|
|
|
|
|
|
Set the global log level on all loggers instantiated by txaio. |
|
Begin logging. |
|
Module Contents¶
- class FailedFuture(type_, value, traceback)[source]¶
Bases:
txaio.interfaces.IFailedFutureThis 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.
- class _AsyncioApi(config)[source]¶
Bases:
object- add_callbacks(future, callback, errback)[source]¶
callback or errback may be None, but at least one must be non-None.
- 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).
- 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.
- 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.
- class _TxaioFileHandler(fileobj, **kw)[source]¶
Bases:
logging.Handler,objectHandler 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.
- class _TxaioLogWrapper(logger)[source]¶
Bases:
txaio.interfaces.ILoggerThis 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
messageargument is a format string using PEP3101-style references to things from thekwargs. Note that there are also the following keys added to thekwargs:log_timeandlog_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.
- 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)
- 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).