Threads

class pysimpleapp.threads.simple_threads.SimpleThread(name: str, supervisor=None)[source]

*SimpleThread* is an abstract class which will provide the basis for building out the threads provided in pysimpleapp.

Methods which must be specified by children:

  • create_params - creates the parameters which should be used during main execution
  • main - the body of the thread which performs the functionality
  • _control_loop - describes how the class executes the main function

It executes its main function as part of a control loop which is specified by child classes. Before it starts, the parameters can be updated and it can even be ended and prevented from ever running.

Must be initialised with a name, owner, input and output queues.

The operation of the thread is implemented in main() This function should be overridden during implementation.

Parameters are created in create_params() which should be overridden when implemented. Parameters should not be created from anywhere else. Updates to the parameter will be collected after each run of the main function.

Messages are handled based on a lookup table called the address_book. There are some in-built commands which can be easily accessed, but the addresses can be changed easily. To add custom commands to the thread, simply create a function in the class which takes a pysimpleapp.message.Message object and give it a key in the address book.

Example

self.address_book["my_custom_command"] = self.my_custom_command_handler

# During message handling, this will be called as:
self.address_book["my_custom_command"](messsage)
__init__(name: str, supervisor=None)[source]

Create the thread, set up control flags and call _create_params

Parameters:
  • name – Identifier for the thread, could be list of strings depending on communication model
  • owner – Address of object which created the thread (often a ThreadManager)
  • message_queue – Input pipe for messages which the thread is expected to process
class Endpoints

An enumeration.

custom_handler(message: pysimpleapp.message.Message)[source]

Override this function to handle custom user messages. Expect a pysimpleapp.Message object as an input, no return value is expected or handled by default

Good idea: Include a command as a key in a package dictionary to reliably handle custom messages. This will be much clearer than sorting by type, size or looking for a specific key or list value.

end()[source]

Put THREAD_END message in queue

main()[source]

Override this function to provide the thread with instructions.

main provides the functionality for the thread. It may contain heavy computations, IO processing and anything you want to do without having to worry about exactly how long it takes.

Good ideas:

  • Read from parameters to decide on program execution
  • Send pysimpleapp.Message to endpoints where they will be sent to subscribers
  • Handle exceptions to prevent unnecessary failures of the thread

Bad ideas:

  • Writing to parameters during program execution
  • Writing things back to the input_queue
  • Implementing operations which take a long time to complete and expecting fast reactions in changes to parameters or THREAD_STOP commands
publish(package, endpoint=<Endpoints.RESULT: 'result'>, command=<Commands.THREAD_HANDLE: 'THREAD_HANDLE'>)[source]

Publishes a package to all the subscribers of an endpoint

By default, will publish to the RESULT endpoint with a command of THREAD_HANDLE. You may wish to provide a different endpoint or command depending on how the application is set up.

raise_exception(exception: Exception)[source]

Notify the supervisor that an exception occurred during thread running

Passes same information as what is received by threading.excepthook

run()[source]

This defines the programatic flow for the thread.

Threads wait for messages on the input_queue and then process them.

If an end command is given, the thread will return True and exit, therefore no longer being able to run.

If a start command is given and no end command is requested, the _control_loop function will be called. While running, the queue will not be processed.

If the control loop raises an exception, the thread will exit gracefully.

send_to(receiver: List[str], command: str, package: any)[source]

Helper function for sending information from a thread correctly

setup()[source]

Override this function to do whatever setup is necessary for the thread. Example:

self.parameters["param 1"] = 0
self.parameters["param 2"] = "some string"
start()[source]

Put THREAD_START message in queue

stop()[source]

Put THREAD_STOP message in queue

Single Run Thread

class pysimpleapp.threads.simple_threads.SingleRunThread(name: str, supervisor=None)[source]

*SingleRunThread* is a child of *SimpleThread* which runs the main function once and then ends.

It will execute after a start command and then exit.

main and create_params are left as abstract methods for the user to implement.

Multi Run Thread

class pysimpleapp.threads.simple_threads.MultiRunThread(name: str, supervisor=None)[source]

*MultiRunThread* is a child of *SimpleThread* which may run the main function repeatedly.

It will execute after a start command, and then await further instruction. This may be another start command, in which case the main function will repeat. Or it may be an update or end command, which will cause the parameters to update or the thread to end, respectively.

Other than that, it works exactly the same way as the *SimpleThread*.

main and _create_params are left as abstract methods for the user to implement.

Repeating Thread

class pysimpleapp.threads.simple_threads.RepeatingThread(name: str, interval: datetime.timedelta = datetime.timedelta(seconds=1))[source]

*RepeatingThread* is a child of *SimpleThread* which runs its main function on a regular basis.

It differs from *MultiRunThread* in that once it has been started, it will trigger its own runs. It may be stopped with the “THREAD_STOP” command. Having received such a command, the thread will be waiting to start again and will not end without a THREAD_END command.

RepeatingThread has a parameter of “loop_timer” which specifies the time, in seconds, after program execution to wait before running main again. Default is 1s.

This is achieved by adding a start message to its own input queue after “loop_timer” amount of time. Therefore, it is susceptible to being flooded by other messages. It may also be triggered by sending another start command.

So, to use safely, set parameters before starting and do not flood with parameters while running. Issue a THREAD_STOP command before performing large changes to the parameters.

main and create_params are left as abstract methods for the user to implement.

repeating_start()[source]

Command to set the main function going again

Precise Repeating Thread