kiwipy.communications module

class kiwipy.communications.Communicator[source]

Bases: object

The interface for a communicator used to both send and receive various types of message.

abstract add_broadcast_subscriber(subscriber: Callable[[kiwipy.communications.Communicator, Any, Any, Any, Any], Any], identifier=None)Any[source]

Add a broadcast subscriber that will receive all broadcast messages

Parameters
  • subscriber – the subscriber function to be called

  • identifier – an optional identifier for the subscriber

Returns

an identifier for the subscriber and can be subsequently used to remove it

abstract add_rpc_subscriber(subscriber: Callable[[kiwipy.communications.Communicator, Any], Any], identifier=None)Any[source]

Add an RPC subscriber to the communicator with an optional identifier. If an identifier is not provided the communicator will generate a unique one. In all cases the identifier will be returned.

abstract add_task_subscriber(subscriber: Callable[[kiwipy.communications.Communicator, Any], Any], identifier=None)Any[source]

Add a task subscriber to the communicator’s default queue. Returns the identifier.

Parameters
  • subscriber – The task callback function

  • identifier – the subscriber identifier

abstract broadcast_send(body, sender=None, subject=None, correlation_id=None)bool[source]

Broadcast a message to all subscribers

abstract close()[source]

Close a communicator, free up all resources and do not allow any further operations

abstract is_closed()bool[source]

Return True if the communicator was closed

abstract remove_broadcast_subscriber(identifier)[source]

Remove a broadcast subscriber

Parameters

identifier – the identifier of the subscriber to remove

abstract remove_rpc_subscriber(identifier)[source]

Remove an RPC subscriber given the identifier. Raises a ValueError if there is no such subscriber.

Parameters

identifier – The RPC subscriber identifier

abstract remove_task_subscriber(identifier)[source]

Remove a task subscriber from the communicator’s default queue.

Parameters

identifier – the subscriber to remove

Raises

ValueError if identifier does not correspond to a known subscriber

abstract rpc_send(recipient_id, msg)[source]

Initiate a remote procedure call on a recipient. This method returns a future representing the outcome of the call.

Parameters
  • recipient_id – The recipient identifier

  • msg – The body of the message

Returns

A future corresponding to the outcome of the call

Return type

kiwipy.Future

abstract task_send(task, no_reply=False)concurrent.futures._base.Future[source]

Send a task messages, this will be queued and picked up by a worker at some point in the future. The method returns a future representing the outcome of the task.

Parameters
  • task – The task message

  • no_reply (bool) – Do not send a reply containing the result of the task

Returns

A future corresponding to the outcome of the task

class kiwipy.communications.CommunicatorHelper[source]

Bases: kiwipy.communications.Communicator

_ensure_open()[source]
add_broadcast_subscriber(subscriber: Callable[[kiwipy.communications.Communicator, Any, Any, Any, Any], Any], identifier=None)Any[source]

Add a broadcast subscriber that will receive all broadcast messages

Parameters
  • subscriber – the subscriber function to be called

  • identifier – an optional identifier for the subscriber

Returns

an identifier for the subscriber and can be subsequently used to remove it

add_rpc_subscriber(subscriber, identifier=None)Any[source]

Add an RPC subscriber to the communicator with an optional identifier. If an identifier is not provided the communicator will generate a unique one. In all cases the identifier will be returned.

add_task_subscriber(subscriber, identifier=None)[source]

Register a task subscriber

Parameters
  • subscriber – The task callback function

  • identifier – the subscriber identifier

close()[source]

Close a communicator, free up all resources and do not allow any further operations

fire_broadcast(body, sender=None, subject=None, correlation_id=None)[source]
fire_rpc(recipient_id, msg)[source]
fire_task(msg, no_reply=False)[source]
is_closed()bool[source]

Return True if the communicator was closed

remove_broadcast_subscriber(identifier)[source]

Remove a broadcast subscriber

Parameters

identifier – the identifier of the subscriber to remove

remove_rpc_subscriber(identifier)[source]

Remove an RPC subscriber given the identifier. Raises a ValueError if there is no such subscriber.

Parameters

identifier – The RPC subscriber identifier

remove_task_subscriber(identifier)[source]

Remove a task subscriber

Parameters

identifier – the subscriber to remove

Raises

ValueError if identifier does not correspond to a known subscriber