spinnman.connections package

Subpackages

Module contents

class spinnman.connections.ConnectionListener(connection: Listenable[T], n_processes: int = 4, timeout: float = 1)

Bases: Thread, Generic[T]

Thread that listens to a connection and calls callbacks with new messages when they arrive.

Parameters:
  • connection (Listenable) – A connection to listen to

  • n_processes (int) – The number of threads to use when calling callbacks

  • timeout (float) – How long to wait for messages before checking to see if the connection is to be terminated.

add_callback(callback: Callable[[T], None])[source]

Add a callback to be called when a message is received.

Parameters:

callback (Callable) – A callable which takes a single parameter, which is the message received; the result of the callback will be ignored.

close() None[source]

Closes the listener.

Note

This does not close the provider of the messages; this instead marks the listener as closed. The listener will not truly stop until the get message call returns.

run() None[source]

Implements the listening thread.

class spinnman.connections.SCPRequestPipeLine(connection: SCAMPConnection, n_channels=1, intermediate_channel_waits=0, n_retries=10, packet_timeout=1.0, non_fail_retry_codes=None)

Bases: Generic[R]

Allows a set of SCP requests to be grouped together in a communication across a number of channels for a given connection.

This class implements an SCP windowing, first suggested by Andrew Mundy. This extends the idea by having both send and receive windows. These are represented by the n_channels and the intermediate_channel_waits parameters respectively. This seems to help with the timeout issue; when a timeout is received, all requests for which a reply has not been received can also timeout.

Parameters:
  • connection (SCAMPConnection) – The connection over which the communication is to take place

  • n_channels (int) – The number of requests to send before checking for responses. If None, this will be determined automatically

  • intermediate_channel_waits (int) – The number of outstanding responses to wait for before continuing sending requests. If None, this will be determined automatically

  • n_retries (int) – The number of times to resend any packet for any reason before an error is triggered

  • packet_timeout (float) – The number of elapsed seconds after sending a packet before it is considered a timeout.

  • non_fail_retry_codes (set(SCPResult) or None) – Codes that could retry but won’t fail, or None if there are no such codes

finish() None[source]

Indicate the end of the packets to be sent. This must be called to ensure that all responses are received and handled.

property n_channels: int

The number of requests to send before checking for responses.

Return type:

int

property n_resent: int

The number of packets that have been resent.

Return type:

int

property n_retry_code_resent: int

The number of resends due to reasons for which automated retry is the correct response in-protocol.

Return type:

int

property n_timeouts: int

The number of timeouts that occurred.

Return type:

int

send_request(request: AbstractSCPRequest[R], callback: Callable[[R], None] | None, error_callback: Callable[[AbstractSCPRequest[R], Exception, TracebackType, SCAMPConnection], None])[source]

Add an SCP request to the set to be sent.

Parameters:
  • request (AbstractSCPRequest) – The SCP request to be sent

  • callback (Callable) – A callback function to call when the response has been received; takes a SCPResponse as a parameter, or None if the response doesn’t need to be processed

  • error_callback (Callable) – A callback function to call when an error is found when processing the message; takes the original AbstractSCPRequest, the exception caught and a list of tuples of (filename, line number, function name, text) as a traceback

class spinnman.connections.TokenBucket(tokens, fill_rate)

Bases: object

An implementation of the token bucket algorithm. Usage:

>>> bucket = TokenBucket(80, 0.5)
>>> print(bucket.consume(10))
True

Not thread safe.

Parameters:
  • tokens (int) – the total tokens in the bucket

  • fill_rate (float) – the rate in tokens/second that the bucket will be refilled.

consume(tokens, block=True)[source]

Consume tokens from the bucket. Returns True if there were sufficient tokens.

If there are not enough tokens and block is True, sleeps until the bucket is replenished enough to satisfy the deficiency.

If there are not enough tokens and block is False, returns False.

It is an error to consume more tokens than the bucket capacity.

Parameters:
  • tokens (int) –

  • block (bool) –

Return type:

bool

property tokens

The number of tokens currently in the bucket.

Return type:

int