Launch a new thread that will repeatedly execute a function in parallel with your Sketch code.


 1def pick_color():
 2    global color
 3    color = py5.random_int(255), py5.random_int(255), py5.random_int(255)
 6def setup():
 7    py5.launch_repeating_thread(pick_color, name='pick_color', time_delay=1)
10def draw():
11    py5.background(*color)
12    if py5.frame_count == 500:
13        py5.stop_thread('pick_color')


Launch a new thread that will repeatedly execute a function in parallel with your Sketch code. This can be useful for executing non-py5 code that would otherwise slow down the animation thread and reduce the Sketch’s frame rate.

Use the time_delay parameter to set the time in seconds between one call to function f and the next call. Set this parameter to 0 if you want each call to happen immediately after the previous call finishes. If the function f takes longer than expected to finish, py5 will wait for it to finish before making the next call. There will not be overlapping calls to function f.

The name parameter is optional but useful if you want to monitor the thread with other methods such as has_thread(). If the provided name is identical to an already running thread, the running thread will first be stopped with a call to stop_thread() with the wait parameter equal to True.

Use the args and kwargs parameters to pass positional and keyword arguments to the function.

Use the daemon parameter to make the launched thread a daemon that will run without blocking Python from exiting. This parameter defaults to True, meaning that function execution can be interupted if the Python process exits. Note that if the Python process continues running after the Sketch exits, which is typically the case when using a Jupyter Notebook, this parameter won’t have any effect unless if you try to restart the Notebook kernel. Generally speaking, setting this parameter to False causes problems but it is available for those who really need it. See stop_all_threads() for a better approach to exit threads.

The new thread is a Python thread, so all the usual caveats about the Global Interpreter Lock (GIL) apply here.


launch_repeating_thread(f: Callable, name: str = None, *, time_delay: float = 0, daemon: bool = True, args: tuple = None, kwargs: dict = None) -> str


  • args: tuple = None - positional arguments to pass to the given function

  • daemon: bool = True - if the thread should be a daemon thread

  • f: Callable - function to call in the launched thread

  • kwargs: dict = None - keyword arguments to pass to the given function

  • name: str = None - name of thread to be created

  • time_delay: float = 0 - time delay in seconds between calls to the given function

Updated on March 01, 2022 12:15:01pm UTC