Util
Utility functions for odin-control.
This module implements utility methods for odin-control.
decode_request_body(request)
Decode the body of an HTTP request object.
This function decodes the body of the provided request object. If the request headers indicate that the request is of type "application/json", the body will be decoded from JSON. Otherwise, the body will be returned as-is.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
HTTP request object |
required |
Returns:
| Type | Description |
|---|---|
|
Decoded request body, either as a Python object if JSON or raw bytes otherwise |
Source code in src/odin_control/util.py
get_async_event_loop()
Get the asyncio event loop.
This function obtains and returns the current asyncio event loop. If no loop is present, a new one is created and set as the event loop.
Returns:
| Type | Description |
|---|---|
|
an asyncio event loop |
Source code in src/odin_control/util.py
run_async(func, *args, **kwargs)
Run an async function synchronously in an event loop.
This function can be used to run an async function synchronously, i.e. without the need for an await() call. The function is run on an asyncio event loop and the result is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
async function to run |
required | |
args
|
positional arguments to function |
()
|
|
kwargs
|
: keyword arguments to function |
{}
|
Returns:
| Type | Description |
|---|---|
|
result of function |
Source code in src/odin_control/util.py
run_in_executor(executor, func, *args)
Run a function asynchronously in an executor.
This method extends the behaviour of Tornado IOLoop equivalent to allow nested task execution without having to modify the underlying asyncio loop creation policy on python 3. If the current execution context does not have a valid IO loop, a new one will be created and used. The method returns a tornado Future instance, allowing it to be awaited in an async method where applicable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
executor
|
a concurrent.futures.Executor instance to run the task in |
required | |
func
|
the function to execute |
required | |
args
|
list of arguments to pass to the function |
()
|
Returns:
| Type | Description |
|---|---|
|
a Future wrapping the task |