Executor API

Executor Interface

class pymoa_remote.executor.ExecutorBase

Bases: object

Executor that can be used to execute a method in a different context, e.g. in a different thread or across the network in a server.

It is not safe to be called concurrently, except execute/execute_generator.

TODO: test that all obj methods raise exception if called before ensure TODO: an error should be raised if using executor outside with block

await apply_config_from_remote(obj)
await apply_data_from_remote(obj, trigger_names: Iterable[str] = (), triggered_logged_names: Iterable[str] = (), logged_names: Iterable[str] = (), initial_properties: Iterable[str] = (), task_status=TASK_STATUS_IGNORED)
await apply_execute_from_remote(obj, exclude_self=True, task_status=TASK_STATUS_IGNORED)
await apply_property_data_from_remote(obj: Any, properties: List[str])
classmethod call_execute_callback(obj, return_value, callback)
classmethod call_execute_callback_func(return_value, callback)
decode(data)
await delete_remote_instance(obj)
encode(data)
await ensure_remote_instance(obj, hash_name, *args, auto_register_class=True, **kwargs) bool
await execute(obj, fn: Union[Callable, str], args=(), kwargs=None, callback: Optional[Union[Callable, str]] = None)
await execute_generator(obj, gen: Union[Callable, str], args=(), kwargs=None, callback: Optional[Union[Callable, str]] = None, task_status=TASK_STATUS_IGNORED) AsyncGenerator
await get_channel_from_remote(hash_name: str, channel: str, task_status=TASK_STATUS_IGNORED) AsyncContextManager[AsyncGenerator]
await get_data_from_remote(obj, trigger_names: Iterable[str] = (), triggered_logged_names: Iterable[str] = (), logged_names: Iterable[str] = (), initial_properties: Iterable[str] = (), task_status=TASK_STATUS_IGNORED) AsyncContextManager[AsyncGenerator]
await get_echo_clock() Tuple[int, int, int]
classmethod get_execute_callback_func(obj, callback)
await get_remote_object_config(obj: Optional[Any])
await get_remote_object_property_data(obj: Any, properties: List[str]) dict
await get_remote_objects()
is_remote = True
name = 'Executor'
await register_remote_class(cls)
await remote_import(module)
async with remote_instance(obj, hash_name, *args, auto_register_class=True, **kwargs) AsyncContextManager[Any]
await sleep(duration=None, deadline=None) int
await start_executor()
await stop_executor()
supports_coroutine = False
supports_non_coroutine = True
class pymoa_remote.executor.InstanceRegistry(**kwargs)

Bases: object

Registry that contains objects know by the register that can be referenced.

It registers classes for instantiation and uses a base64 name hash for identifying them. It registers coders for serializing them.

convert_base64_to_hash(encoded_hash: Union[bytes, str]) str
convert_hash_to_base64(hash_val: str) str
decode_json(data: str)
decode_json_buffers(data: bytes, json_bytes: int, num_buffers: int)
decode_json_buffers_header(header: bytes)
decode_json_buffers_raw(data: bytes)
encode_json(obj) str
encode_json_buffers(obj) bytes

Message is: magic number, size of dynamic message, size of json, number of buffers, json, list of size for each buffer, buffers.

encode_json_func(obj, buffers: Optional[list] = None)
hashed_instances: Dict[str, Any] = {}
hashed_instances_ids: Dict[int, str] = {}
is_class_registered(class_to_register: Optional[type] = None, class_triple=None)
json_coders: Dict[str, Tuple[type, Callable, Callable]] = {}
prepare_json_buffers(obj) Tuple[bytes, List[bytes]]
referenceable_classes: Dict[Tuple[str, str, str], Callable] = {}
referenceable_json_decoder(dct: dict, buffers: Optional[list] = None)
register_class(class_to_register: type, triple=None)

Duplicated register raises error.

register_json_coder(name: str, class_to_register: type, encoder: Callable, decoder: Callable)
pymoa_remote.executor.NO_CALLBACK = '#@none'

Can be used with apply_executor() to indicate that no callback should be used.

Executor Client API

class pymoa_remote.client.Executor(registry: Optional[pymoa_remote.client.LocalRegistry] = None, init_context=True, **kwargs)

Bases: pymoa_remote.executor.ExecutorBase

Executor base class used by all the clients.

Adds internal API used by the clients to help it have a uniform public API.

decode(data: str) Any

Decodes the data encoded with encode().

encode(data: Any) str

Encodes the data as required by the specific executor.

registry: pymoa_remote.client.LocalRegistry = None

The registry that tracks registered objects and classes.

class pymoa_remote.client.ExecutorContext(executor: pymoa_remote.client.Executor, **kwargs)

Bases: object

Context manager that sets the executor to be used when a executor decorated method is called. E.g. given:

class Demo:

    @apply_executor
    def sum(self, a, b):
        return a + b

When calling result = await demo.sum(1, 2), since no executor is set, the method will be executed locally. ExecutorContext sets the current executor e.g.:

with ExecutorContext(executor):
    result = await demo.sum(1, 2)

will use the executor to execute sum remotely with that executor. Multiple executors can be used as needed e.g.:

with ExecutorContext(executor_1):
    result1 = await demo.sum(1, 2)

    with ExecutorContext(executor_2):
        result2 = await demo.sum(1, 2)

    result3 = await demo.sum(1, 2)

will use executor_1 to compute result1 and result3 and executor_2 for result2.

executor: pymoa_remote.client.Executor

The context’s executor.

token = None
class pymoa_remote.client.LocalRegistry(**kwargs)

Bases: pymoa_remote.executor.InstanceRegistry

Client side object registry.

add_instance(obj, hash_name)

Registers the object using the given name.

Parameters
  • obj – The object to register

  • hash_name – The name to use to identify this object by the executor and its remote server (if applicable).

Returns

The object.

Raises

ValueError if the name is already registered.

delete_instance(obj)

Removes the previously registered object.

Parameters

obj – The object previously registered with add_instance().

Returns

The object.

pymoa_remote.client.apply_executor(func=None, callback=None)

Decorator that will cause the method to be executed remotely using the currently active executor and return its value.

Parameters
  • func – The method to be decorated.

  • callback – The optional callback that will be executed with the method result. This can be specified as a string or method, but it must be a method of the same class.

Returns

The decorated method.

pymoa_remote.client.apply_generator_executor(func=None, callback=None)

Decorator that calls the generator method using the executor.