_thread — Low-level threading API


This module provides low-level primitives for working with multiple threads (also called light-weight processes or tasks) — multiple threads of control sharing their global data space. For synchronization, simple locks (also called mutexes or binary semaphores) are provided. The threading module provides an easier to use and higher-level threading API built on top of this module.

Changed in version 3.7: This module used to be optional, it is now always available.

This module defines the following constants and functions:

exception _thread.error

Raised on thread-specific errors.

Changed in version 3.3: This is now a synonym of the built-in RuntimeError.

_thread.LockType

This is the type of lock objects.

_thread.start_new_thread(function, args[, kwargs])

Start a new thread and return its identifier. The thread executes the function function with the argument list args (which must be a tuple). The optional kwargs argument specifies a dictionary of keyword arguments.

When the function returns, the thread silently exits.

When the function terminates with an unhandled exception, sys.unraisablehook() is called to handle the exception. The object attribute of the hook argument is function. By default, a stack trace is printed and then the thread exits (but other threads continue to run).

When the function raises a SystemExit exception, it is silently ignored.

Raises an auditing event _thread.start_new_thread with arguments function, args, kwargs.

Changed in version 3.8: sys.unraisablehook() is now used to handle unhandled exceptions.

_thread.interrupt_main(signum=signal.SIGINT, /)

Simulate the effect of a signal arriving in the main thread. A thread can use this function to interrupt the main thread, though there is no guarantee that the interruption will happen immediately.

If given, signum is the number of the signal to simulate. If signum is not given, signal.SIGINT is simulated.

If the given signal isn’t handled by Python (it was set to signal.SIG_DFL or signal.SIG_IGN), this function does nothing.

Changed in version 3.10: The signum argument is added to customize the signal number.

Note

This does not emit the corresponding signal but schedules a call to the associated handler (if it exists). If you want to truly emit the signal, use