_thread — Threading
This module provides low-level primitives for dealing with multiple threads (also called lightweight processes or tasks) - multiple threads of control sharing the same global data space. For synchronization, simple locks (also called mutexes or binary semaphores) are provided.
When a thread-specific error occurs, a RuntimeError exception is raised.
Quick usage example:
import _thread
import time
def th_func(delay, id):
while True:
time.sleep(delay)
print('Running thread %d' % id)
for i in range(2):
_thread.start_new_thread(th_func, (i + 1, i))
Methods
- _thread.start_new_thread(function, args[, kwargs])
Start a new thread and return its identifier. The thread executes the function using the argument list args (which must be a tuple). The optional kwargs parameter specifies a dictionary of keyword arguments. When the function returns, the thread exits silently. When the function terminates with an unhandled exception, a stack trace is printed and then the thread exits (but other threads continue to run).
- _thread.exit()
Raise the SystemExit exception. If it is not caught, this will cause the thread to exit silently.
- _thread.allocate_lock()
Return a new lock object. The methods of the lock are described below. The lock is initially in the unlocked state.
- _thread.get_ident()
Return the thread identifier of the current thread. This is a non-zero integer. Its value has no direct meaning; it is intended to be used as a magic cookie, for example as an index into a dictionary of thread-specific data. Thread identifiers may be recycled when a thread exits and another thread is created.
- _thread.stack_size([size])
Return the thread stack size used when creating new threads (in bytes). The optional size parameter specifies the stack size used for subsequently created threads, and must be 0 (use the platform or configured default) or a positive integer value of at least 4096 (4KiB). 4KiB is currently the minimum supported stack size value, to ensure that the interpreter itself has enough stack space.
Objects
- _thread.LockType
This is the type of lock objects.
Lock Class
- class Lock
The threading module provided by Python contains an easy-to-implement locking mechanism that enables synchronization between threads. Create a new lock by calling the Lock() method. The acquire (blocking) method of a new lock object is used to force threads to run synchronously. The optional blocking parameter lets you control whether the thread waits to acquire the lock.
Methods
Lock objects have the following methods:
- lock.acquire(waitflag=1, timeout=-1)
With no optional arguments, this method acquires the lock unconditionally, waiting if necessary until it is released by another thread (only one thread can acquire a lock at a time - that is why they exist).
If the integer waitflag argument is present, the action depends on its value: if it is zero, the lock is only acquired if it can be acquired immediately without waiting, while if it is non-zero, the lock is acquired unconditionally as described above.
If the floating-point timeout argument is present and positive, it specifies the maximum wait time before returning (in seconds). A negative timeout argument specifies an unbounded wait. If waitflag is zero, the timeout cannot be specified.
Returns the value True if the lock is acquired successfully, otherwise returns the value False.
- lock.release()
Release the lock. The lock must have been acquired first, but not necessarily by the same thread.
- lock.locked()
Return the status of the lock: True if it has been acquired by some thread, False if not.
In addition to these methods, lock objects can also be used with the with statement, for example:
import _thread
a_lock = _thread.allocate_lock()
with a_lock:
print("a_lock is locked while this executes")