aiomas.util

This module contains some utility functions.

aiomas.util.arrow_serializer()[source]

Return a serializer for arrow dates.

The return value is an argument tuple for aiomas.codecs.Codec.add_serializer().

aiomas.util.async(coro_or_future, ignore_cancel=True, loop=None)[source]

Run asyncio.async() with coro_or_future and set a callback that instantly raises all exceptions.

If ignore_cancel is left True, no exception is raised if the task was canceled. If you also want to raise the CancelledError, set the flag to False..

Return an asyncio.Task object.

The difference between this function and asyncio.async() subtle, but important if an error is raised by the task:

asyncio.async() returns a future (asyncio.Task is a subclass of asyncio.Future) for the task that you created. By the time that future goes out of scope, asyncio checks if someone was interested in its result or not. If the result was never retrieved, the exception is printed to stderr.

If you call it like asyncio.async(mytask()) (note that we don’t keep a reference to the future here), an exception in mytask will pre printed immediately when the task is done. If, however, we store a reference to the future (fut = asyncio.async(mytask())), the exception only gets printed when fut goes out of scope. That means if, for example, an Agent creates a task and stores it as an instance attribute, our system may keep running for a long time after the exception has occured (or even block forever) and we won’t see any stacktrace. This is because the reference to the task is still there and we could, in theory, still retrieve the exception from there.

Since this can make debugging very hard, this method simply registers a callback to the future. The callback will try to get the result from the future when it is done and will thus print any exceptions immediately.

aiomas.util.run(until=None)[source]

Run the event loop forever or until the task/future until is finished.

This is an alias to asyncio’s run_forever() if until is None and to run_until_complete() if not.

aiomas.util.make_ssl_server_context(cafile, certfile, keyfile)[source]

Return an ssl.SSLContext that can be used by a server socket.

The server will use the certificate in certfile and private key in keyfile (both in PEM format) to authenticate itself.

It requires clients to also authenticate themselves. Their certificates will be validated with the root CA certificate in cafile.

It will use TLS 1.2 with ECDH+AESGCM encryption. ECDH keys won’t be reused in distinct SSL sessions. Compression is disabled.

aiomas.util.make_ssl_client_context(cafile, certfile, keyfile)[source]

Return an ssl.SSLContext that can be used by a client socket.

It uses the root CA certificate in cafile to validate the server’s certificate. It will also check the server’s hostname.

The client will use the certificate in certfile and private key in keyfile (both in PEM format) to authenticate itself.

It will use TLS 1.2 with ECDH+AESGCM encryption.