stop_thread()

Stop a thread of a given name.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
def pick_color():
    global color
    color = py5.random_int(255), py5.random_int(255), py5.random_int(255)


def setup():
    py5.launch_repeating_thread(pick_color, name='pick_color', time_delay=1)


def draw():
    py5.background(*color)
    if py5.frame_count == 500:
        py5.stop_thread('pick_color')

Description

Stop a thread of a given name. The wait parameter determines if the method call will return right away or wait for the thread to exit.

This won’t do anything useful if the thread was launched with either launch_thread() or launch_promise_thread() and the wait parameter is False. Non-repeating threads are executed once and will stop when they complete execution. Setting the wait parameter to True will merely block until the thread exits on its own. Killing off a running thread in Python is complicated and py5 cannot do that for you. If you want a thread to perform some action repeatedly and be interuptable, use launch_repeating_thread() instead.

Use has_thread() to determine if a thread of a given name exists and list_threads() to get a list of all thread names. Use stop_all_threads() to stop all threads.

Syntax

stop_thread(name: str, wait: bool = False) -> None

Parameters

  • name: str - name of thread

  • wait: bool = False - wait for thread to exit before returning

Updated on September 11, 2021 16:51:34pm UTC