Python Glossary Русский

all Python 3threadingterm

threading.Thread

threads

The class for creating and starting a thread. Threads share the memory of the process. Because of the GIL it does not speed up CPU-bound work, but it is good for I/O.

Syntax

t = threading.Thread(target=func, args=(), kwargs={}, daemon=False)
t.start()
t.join()

Start a thread with start(); calling t.run() directly runs the target synchronously in the current thread with no parallelism at all. A second start() on the same object raises RuntimeError, and daemon=True threads are killed abruptly at interpreter exit, skipping finally blocks and cleanup.

Examples

import threading
def worker(n):
    print(f'поток {n}')
threads = [threading.Thread(target=worker, args=(i,)) for i in range(3)]
for t in threads:
    t.start()
for t in threads:
    t.join()  # ждать завершения

See also

Open in the full glossarydocs.python.orgReport an inaccuracy