nspyre.gui.threadsafe

Module Contents

Classes

QThreadSafeObject

Qt object associated with a new QThread. Implements several methods to

Functions

run_threadsafe([blocking])

Decorate QThreadSafeObject sublass methods with this in order to automatically

nspyre.gui.threadsafe.run_threadsafe(blocking=False)

Decorate QThreadSafeObject sublass methods with this in order to automatically run them using run_safe.

Usage Example:

from nspyre import QThreadSafeObject
from nspyre import run_threadsafe

class DataBackend(QThreadSafeObject):
    @run_threadsafe()
    def doSomethingThreadSafe(self, arg):
        # this will be run in the QThreadSafeObject thread
        return arg
Parameters:

blocking – see run_safe().

class nspyre.gui.threadsafe.QThreadSafeObject

Qt object associated with a new QThread. Implements several methods to make it easier to work with data in a thread-safe way.

mutex

Mutex to lock access to instance variables.

thread

Thread to manage access requests for this object.

start()

Start the internal thread to handle requests.

stop()

Quit the internal thread.

run_main(fun, *args, blocking=False, **kwargs)

Run the given function on the main thread.

Parameters:
  • fun – Function to run.

  • args – Arguments to the function.

  • kwargs – Keyword arguments to the function.

  • blocking – If true, block until the function returns. Non-blocking calls cannot return anything.

Returns:

Return value of the function.

run_safe(fun, *args, blocking=False, **kwargs)

Run a given function on the thread of this object.

Parameters:
  • fun (Callable) – Function to run.

  • args – Arguments to the function.

  • kwargs – Keyword arguments to the function.

  • blocking – If true, block until the function returns. Non-blocking calls cannot return anything.

Returns:

Return value of the function.

get_safe(attrs)

Retrieve object attributes in a thread-safe way by accessing them while holding the internal mutex.

Parameters:

attrs (list[str]) – Object attribute names.

Returns:

tuple of object attributes.

Return type:

tuple