traits_futures.multithreading_router module

Implementations of the IMessageSender, IMessageReceiver and IMessageRouter interfaces for tasks executed on a background thread.

class traits_futures.multithreading_router.MultithreadingReceiver[source]

Bases: traits.has_traits.HasStrictTraits

Implementation of the IMessageReceiver interface for the case where the sender will be in a background thread.

connection_id = Int()

Connection id, matching that of the paired sender.

message = Event(Any())

Event fired when a message is received from the paired sender.

class traits_futures.multithreading_router.MultithreadingRouter(**traits)[source]

Bases: traits.has_traits.HasRequiredTraits

Implementation of the IMessageRouter interface for the case where the sender will be in a background thread.

Parameters

gui_context (IGuiContext) – GUI context to use for interactions with the GUI event loop.

close_pipe(receiver)[source]

Close the receiver end of a pipe produced by pipe.

Removes the receiver from the routing table, so that no new messages can reach that receiver.

Not thread safe. Must always be called in the main thread.

Parameters

receiver (MultithreadingReceiver) – Receiver half of the pair returned by the pipe method.

Raises

RuntimeError – If the router is not currently running.

gui_context = self._route_message)

GUI context to use for interactions with the GUI event loop.

pipe()[source]

Create a (sender, receiver) pair for sending and receiving messages.

The sender will be passed to the background task and used to send messages, while the receiver remains in the foreground.

Not thread safe. Must always be called in the main thread.

Returns

  • sender (MultithreadingSender) – Object to be passed to the background task.

  • receiver (MultithreadingReceiver) – Object kept in the foreground, which reacts to messages.

Raises

RuntimeError – If the router is not currently running.

start()[source]

Start routing messages.

This method must be called before any call to pipe or close_pipe can be made.

Not thread-safe. Must always be called in the main thread.

Raises

RuntimeError – If the router has already been started.

stop()[source]

Stop routing messages.

This method should be called in the main thread after all pipes are finished with. Calls to pipe or close_pipe are not permitted after this method has been called.

Logs a warning if there are unclosed pipes.

Not thread safe. Must always be called in the main thread.

Raises

RuntimeError – If the router is not running.

class traits_futures.multithreading_router.MultithreadingSender(connection_id, pingee, message_queue)[source]

Bases: object

Object allowing the worker to send messages.

This class will be instantiated in the main thread, and the instance passed to the worker thread to allow the worker to communicate back to the main thread.

Parameters
  • connection_id (int) – Id of the matching receiver; used for message routing.

  • pingee (IPingee) – Recipient for pings, used to notify the GUI thread that there’s a message pending.

  • message_queue (queue.Queue) – Thread-safe queue for passing messages to the foreground.

send(message)[source]

Send a message to the router.

Parameters
  • message (object) – Typically this will be immutable, small, and pickleable.

  • thread-safe. The 'start' (Not) –

  • and 'stop' methods should ('send') –

  • be called from the same thread. (all) –

Raises

RuntimeError – If the sender has not been started, or has already been stopped.

start()[source]

Do any setup necessary to prepare for sending messages.

This method must be called before any messages can be sent using the send method.

Not thread-safe. The ‘start’, ‘send’ and ‘stop’ methods should all be called from the same thread.

Raises

RuntimeError – If the sender has previously been started.

stop()[source]

Do any teardown.

After this method has been called, no more messages can be sent.

Not thread-safe. The ‘start’, ‘send’ and ‘stop’ methods should all be called from the same thread.

Raises

RuntimeError – If the sender has not been started, or has already been stopped.