spinnman.connections package

Module contents

class spinnman.connections.ConnectionListener(connection, n_processes=4, timeout=1)

Bases: threading.Thread, spinn_utilities.abstract_context_manager.AbstractContextManager

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

  • 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 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.

Closes the listener.


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.


Implements the listening thread.

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

Bases: object

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.

  • 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.

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


The number of requests to send before checking for responses.

Return type:int

The number of packets that have been resent.

Return type:int

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

Return type:int

The number of timeouts that occurred.

Return type:int
send_request(request, callback, error_callback)[source]

Add an SCP request to the set to be sent.

  • 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))

Not thread safe.

  • 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.

  • tokens (int) –
  • block (bool) –
Return type:



The number of tokens currently in the bucket.

Return type:int