puzzlepiece.threads module

class puzzlepiece.threads.CallLater(function, *args, **kwargs)[source]

Bases: object

A callable object that will run a specified function when the next Qt event loop iteration occurs, but only once, irrespective of how many times the CallLater was called since the last iteration of the event loop.

This is useful if you are stuck in a long running function and want to schedule something for when it finishes or when puzzlepiece.puzzle.Puzzle.process_events() is called.

Let’s say a puzzlepiece.piece.Piece has an image view defined in custom_layout() and we’d like to update the image when the Piece’s image ParamArray changes. We can then do:

def update_image():
    image.setImage(self.params['image'].value)
update_later = pzp.threads.CallLater(update_image)
self.params['image'].changed.connect(update_later)

instead of directly connecting update_image. This way the image data will be set only once, when the next GUI update occurs. So the code is more efficient if somewhere else we do:

for i in range(1000):
    self.params['image'].get_value()
    # The image view won't update here as the Qt event loop is stuck inside this function
puzzle.process_events()
# update_image will be called once as part of this event loop iteration,
# instead of a 1000 times.
# You can also directly call the CallLater object:
update_later()

This behaviour is implemented through a single shot QTimer stored internally.

Parameters:
  • function – The function to be called.

  • kwargs (args and) – further arguments and keyword arguments can be provided, they will be passed to the function when it is called.

class puzzlepiece.threads.Worker(function, args=None, kwargs=None)[source]

Bases: QRunnable

Generic worker (QRunnable) that calls a function in a thread.

Parameters:
  • function – The function for the Worker to run.

  • args – list of arguments to be forwarded to the function when run.

  • kwargs – dictionary of keyword arguments to be forwarded to the function when run.

done

Bool flag, True when task finished.

returned

A Qt signal emitted when the function returns, passes the returned value to the connected Slot.

run()[source]

Run the Worker. Shouldn’t be excecuted directly, instead use puzzlepiece.puzzle.Puzzle.run_worker().

class puzzlepiece.threads.LiveWorker(function, sleep=0.1, args=None, kwargs=None)[source]

Bases: Worker

A Worker that calls a function repeatedly in a thread, separated by specified time intervals.

Parameters:
  • function – The function for the Worker to run.

  • sleep – Time to sleep between function calls in seconds.

  • args – list of arguments to be forwarded to the function when run.

  • kwargs – dictionary of keyword arguments to be forwarded to the function when run.

returned

A Qt signal emitted each time the function returns, passes the returned value to the connected Slot.

stop()[source]

Ask the Worker to stop. This will only take effect once the current execution of the function is over.

run()[source]

Run the Worker. Shouldn’t be excecuted directly, instead use puzzlepiece.puzzle.Puzzle.run_worker().

class puzzlepiece.threads.PuzzleTimer(name, puzzle, function, sleep=0.1, args=None, kwargs=None)[source]

Bases: QWidget

A Widget that displays a checkbox. When the checkbox is checked, an associated function is called repeatedly in a thread, separated by specified time intervals.

Parameters:
  • name – The display name for this PuzzleTimer.

  • puzzle – The parent Puzzle (needed as the Puzzle is charged with the QThreadPool that runs the tasks).

  • function – The function for the PuzzleTimer to run.

  • sleep – Time to sleep between function calls in seconds.

  • args – list of arguments to be forwarded to the function when run.

  • kwargs – dictionary of keyword arguments to be forwarded to the function when run.

returned

A Qt signal emitted each time the associated LiveWorker returns, passes the returned value to the connected Slot.

stop()[source]

Ask the PuzzleTimer to stop.

This will only take effect once the current execution of the function is over, see puzzlepiece.threads.LiveWorker.stop().

property sleep

Interval in seconds between executions of the associated function. The property can be modified to change the interval for the associated Worker, even if it’s already running.