The threading module provides a high-level thread interface, based on the low-level _thread module implementation.
Basic methods of modules
The module is determined by the following methods:
Threading.active_count ()
Returns the number of thread objects currently active. The return value and the length of the list returned by enumerate () are equal.
Threading.current_thread ()
Returns the current thread object that corresponds to the caller's control thread. If the caller's control thread is not created through the threading module, a function-constrained virtual thread is returned.
Threading.get_ident ()
Returns the thread identifier of the current thread. This is a non-0 integer that has no specific meaning and is typically used to index thread-specific data in a dictionary. Thread identifiers can be reused.
Threading.enumerate ()
Returns a list of all thread objects that are currently active. The list includes the sprite thread, the virtual thread object created by Current_thread (), and the main thread. It does not include terminated lines rountines accesses threads that have not yet started.
Threading.main_thread ()
Returns the main thread object. Under normal circumstances, the main thread is the one that the Python interpreter initiates.
Threading.settrace (func)
Set up a trace function for all threads started from the threading module. Before each thread's run () method is called, the function is passed to Sys.settrace () for each thread.
Threading.setprofile (func)
Set up a profile function for all threads started from the threading module. Before each thread's run () method is called, the function is passed to Sys.setprofile () for each thread.
Threading.stack_size ([size])
Returns the thread stack size that is used when creating a new thread, and 0 represents the default value for using the platform or configuration.
A constant at the top of the platform is as follows:
Threading. Timeout_max
The maximum allowable value for the timeout parameter of the blocking function (Lock.acquire (), Rlock.acquire (), condition.wait (), etc.). Specifying a value that exceeds this value will throw overflowerror.
The module also defines some classes, which are described below.
The module is designed to emulate the Java threading model. However, Java makes the lock and condition variables the basic behavior of each object, and in Python it is the detached object. The thread class of Python supports a subset of the behavior of the Java threading class; Currently, there is no priority, no thread group, and the thread cannot be destroyed, stopped, paused, resumed, or interrupted. When implemented, the static methods of the Java thread class are mapped to module-level functions.
All the methods described below are executed atomically.
Thread-Local Data
Thread-Local data is those values that are related to a particular thread. To manage thread-local data, create an instance of a local class (or a subclass) and store the attributes inside it:
MyData = threading.local () mydata.x = 1
The values for the different thread instances will be different.
Class Threading.local
A class that represents thread-local data.
For more details, refer to the _threading_local document string.
Thread Object
The thread class represents a behavior that runs in a separate control thread. There are two ways to specify this behavior: by passing a callable object to the constructor, or by overloading the run () method in a subclass, there is no other method in the subclass (except the constructor) that should be overloaded, in other words, overloading only the __init__ () and run () methods of the class.
Once a thread object is created, his behavior must be started by the thread's start () method, which calls the run () method in a separate control thread.
Once the thread's behavior is started, the thread is considered ' active '. Normally, when its run () terminates, it rolls out an active state, or an exception appears to be handled. The Is_alive () method can be used to test whether a thread is active.
Other threads can invoke the join () method of a thread. This blocks the calling thread until the thread that the join () method is called terminates.
A thread has a name that can be passed to the constructor and can be read or changed through the Name property.
A thread can be labeled as "Sprite Thread". The meaning of this flag is that the Python program will exit when there is an elf thread left in the present. The initial value is inherited from the created thread, which can be set through the Daemon property or passed in by the constructor's daemon parameter.
Note that the sprite thread stops abruptly when it shuts down, and their resources (such as open files, database transactions, and so on) cannot be properly freed. If you want your threads to gracefully stop, you should make them non-sprite threads and use an appropriate signaling mechanism, such as event (explained later).
There is a "main thread" object, which corresponds to the initial control thread of the Python program, which is not a sprite thread.
It is possible that a "dummy thread object" is created, then there will be a thread object corresponding to an "alien thread" whose control thread is launched outside of the threading module, for example, directly from C code. The functionality of Virtual threading objects is limited, and they are always considered active sprite threads and cannot be join (). They cannot be removed because it is impossible to detect the termination of an alien thread.
Here's how to construct the thread class:
Class Threading. Thread (Group=none, Target=none, Name=none, args= (), kwargs={}, *, Daemon=none)
1) Group: should be none, reserved for future expansion, when a Threadgroup class is implemented when needed;
2) Target: A callable object, called by the Run () method, defaults to none, meaning nothing is done;
3) Name: The name of the thread, by default, a form of "Thread-n" the unique name is constructed, N is a small decimal number;
4) args: Call Target's parameter tuple, default is ();
5) Kwargs: parameter dictionary called for target, default = {};
6) Daemon: If not none, set whether the thread is a sprite thread. If none, inherits from the current thread;
If a subclass overrides a constructor, it must ensure that the constructor of the base class is called First (thread.__init__ ()) before doing anything else.
The common methods of threading are as follows:
1) Start ()
Start the thread.
Each thread can be called at most once. It causes the object's run () method to be called in a separate control thread.
If you call more than once on the same thread object, RuntimeError will be thrown.
2) Run ()
A method that represents the behavior of a thread.
You can overload the method in a subclass. The standard run () method call is passed to the callable object in the constructor (corresponding to the target parameter), using the corresponding args or Kwargs parameter.
3) Join (Timeout=none)
Wait until the thread ends. This will block the current thread until the thread called by the Join () method terminates or throws an unhandled exception, or the set overflow time arrives.
If the timeout parameter is specified and is not none, it should be a floating-point number that specifies the time in seconds for the operation to overflow. Since join () always returns NONE, you must call Is_alive () at the end of the join () to determine if the thread is ending, and if the line Cheng Jinghan is active, the join () call is a time overflow.
When timeout is not specified, or none is specified, the operation is blocked until the thread terminates.
A thread can be join () multiple times.
If an attempt to join the current thread will result in a deadlock, join () will throw runtimeerror. Doing a join () operation on the thread before a thread starts will also cause the same exception.
4) name
The thread name, used only to identify a thread, has no semantics, multiple threads can be given the same name, and the initial name is set by the constructor.
5) GetName ()
SetName ()
The old Getter/setter API for name is now replaced directly with the name attribute.
6) Ident
"Thread identifier" for this thread, or none if the thread does not start. This is a non-0 integer. Thread identifiers can be recycled, and a thread's thread identifier can be used by other threads after it exits.
7) is_alive ()
Returns whether the thread is active.
This method returns true only after the run () is started and before it terminates. The module function enumerate () returns a list of all active threads.
8) Daemon
A Boolean value that is used to indicate whether the thread is a sprite thread. The value must be set before the start () method call, otherwise RuntimeError is thrown. Its initial value inherits from the thread that was created. The main thread is not an elf thread, so all threads daemon created in the main thread default to False.
When there is no active non-sprite thread running, the entire Python program exits.
9) Isdaemon ()
Setdaemon ()
The old Getter/setter API is now replaced directly with the daemon attribute.
Lock Object
A primitive lock is a synchronization primitive that is not owned by a particular thread. In Python, it is the lowest level of synchronization primitives currently available and is implemented directly through the _thread extension module.
There are two states for a primitive lock, "lock" or "unlocked", which is unlocked when it is initially created. He has two basic methods: Acquire () and release (). When the state is unlocked, acquire () changes its state to the lock and returns immediately; When the state is a lock, acquire () blocks until another thread calls release () releases the lock, and then acquire () acquires the lock and resets the lock's state to the lock and returns. Release () should only be called when the lock is in process lock state, it changes the state of the lock to unlocked and returns immediately. If you try to free an unlocked lock, a runtimeerror will be thrown.
The lock also supports the context management protocol.
When more than one thread is blocked by a lock, only one thread can get to the lock after the lock is released, and the thread that gets to the lock is not deterministic, depending on the specific implementation.
The related classes are as follows:
Class Threading. Lock
This class implements the primitive lock object. The thread can request the lock through acquire, and if another thread has already acquired the lock, the thread blocks until the other thread releases the lock.
1) Acquire (Blocking=true, timeout=-1)
Request a lock, block, or non-blocking.
When the blocking parameter is true (the default), it blocks until the lock is freed, and then acquires the lock and returns TRUE.
When the blocking parameter is false, it is not blocked. If an existing thread acquires a lock, the call returns false immediately, otherwise the lock is acquired and true is returned.
When the timeout parameter is greater than 0 o'clock, the seconds value specified by timeout is blocked. A timeout of 1 (the default) indicates waiting. Specifying the timeout parameter is not allowed when blocking is false.
Returns true if the lock request succeeds, otherwise false (for example, timeouts).
2) Release ()
Release a lock. This can be called in any thread, not only in the thread that acquires the lock.
When the lock is in the lock state, it is unlocked and returned, and the other thread that is blocking the lock waits for a thread to get to the lock.
Calling the method on an unlocked lock will throw runtimeerror.
no return value.
Rlock Object
A reentrant lock can be requested multiple times by the same thread. Internally, it uses the concept of "owner thread" and "recursive level" on the basis of a primitive lock. In the lock state, some threads have locks, and in the unlocked state, no threads have locks.
In order to obtain a lock, a thread calls the acquire () method, acquires a lock and returns it, and a thread calls the release () method in order to release the lock. The call to acquire ()/release () can be nested, only the last release () resets the lock to unlocked.
The reentrant lock also supports the context management protocol.
Class Threading. Rlock
This class implements a Reentrant lock object. A reentrant lock must be released by the thread that requested it, and once a thread has a reentrant lock, the thread can request it again, noting that the number of times the lock must be requested corresponds to the number of times the lock was released.
Note that Rlock is actually a factory function that returns an instance of the most efficient Rlock class version supported by the current platform.
1) Acquire (Blocking=true, timeout=-1)
Request a lock, block, or non-blocking mode.
When the argument is empty: If the thread already owns the lock, the recursion level is added one, and then returns, otherwise, if another thread owns the lock, it blocks until the lock is released. If the lock is in an unlocked state (not owned by any thread), set the owner thread and set the recursive level to 1, and then return. If more than one thread is in the blocking wait queue, only one thread can acquire the lock at a time. There is no return value for this scenario.
When blocking is true, the scene with no arguments is the same and returns True.
When blocking is false, it will not block. Returns false immediately if the lock is in a lock state, otherwise, the scene with no arguments is the same and returns True.
When the timeout parameter is greater than 0 o'clock, it blocks timeout seconds. Returns true if the lock was acquired within timeout seconds, otherwise the time-out returns false.
2) Release ()
Release a lock to reduce the recursion level. If the recursion level is reduced to 0, the state of the lock is reset to unlocked (not owned by any thread). If the lower recursion level is greater than 0, then the lock is still persisted by the caller thread.
This method is called only when the caller thread owns the lock, otherwise throws runtimeerror.
no return value.
Condition Object
Condition variables are always associated with locks, which can be passed in or created by default. Passing in is useful when several condition variables must share the same lock. Locks are part of the condition object: You don't have to track it separately.
The condition variable follows the context Management protocol: Gets the associated lock in the code block with the WITH statement. The Acquire () and release () methods also call the corresponding method of the associated lock.
Other methods must be called by the holder of the associated lock. The wait () method releases the lock and then blocks until another thread calls notify () or Notify_all () wakes it up. After waking, wait () acquires the lock again and returns. It can also specify a time-out.
The Notify () method wakes up one of the waiting threads, and the Notify_all () method wakes up all the waiting threads.
Note: the Notify () and Notify_all () methods do not release locks, which means that the awakened thread or thread group will not return immediately from the wait () call.
A typical application using the condition variable is to use a lock to synchronize access to some shared states, and the thread that is interested in a particular state repeatedly calls wait () until the state in which they are interested is called by the thread that modifies the state, or notify () or Notify_all () To notify the waiting thread that the state has changed. For example, here is a typical producer-consumer pattern that uses an infinite cache:
# Consume an entry with CV: While not an_item_is_available (): cv.wait () Get_an_available_item () # produces an entry with CV: make_an_item_available () cv.notify ()
The while loop checks if an entry is available, because wait () can be returned after waiting any time, or the thread that may call notify () does not make the condition true, and this problem persists in multithreaded programming. The Wait_for () method can be used for automatic condition detection, simplifying the calculation of timeouts:
# Consume an entry with CV: cv.wait_for (an_item_is_available) Get_an_available_item ()
for notify () and Notify_all (), which one or more waiting threads are used in the application scenario. For example, in a typical producer-consumer scenario, adding an entry to the cache only requires waking up a consumer thread.
Class Threading. Condition (Lock=none)
The class implements the condition variable object. A condition variable allows one or more threads to wait until they are notified by another thread.
If the lock parameter is specified and is not none, it must be a lock or Rlock object that is used as an implicit lock. Otherwise, a new Rlock object is created and acts as an implicit lock.
1) Acquire (*args)
Request an implicit lock, this method invokes the corresponding method of the implied lock, and the return value is the return value of the method that implicitly locks.
2) Release ()
Releases the implicit lock. This method invokes the corresponding method of the implied lock, with no return value.
3) Wait (Timeout=none)
Wait until it wakes up, or times out. If the calling thread does not request a lock, RuntimeError is thrown.
The method releases the implicit lock and then blocks until it wakes up by another thread calling the Notify () or Notify_all () method of the same condition object, or until the specified timeout time overflows. Once it wakes up or times out, it re-requests the lock and returns.
When the timeout parameter is specified and is not none, a second-level time-out is specified.
When an implicit lock is a rlock lock, it does not release the lock through the release () method, because if the thread requests multiple locks, the release () method cannot be unlocked (the same number of times must be called and the lock method can be unlocked). The internal interface of a Rlock class is used, which can release a lock regardless of how many times the lock is requested. When a lock is requested, another internal interface is used to restore the recursive level of the lock.
The method returns true if the timeout returns false.
4) wait_for (predicate, timeout=none)
Wait until the condition is true. The predicate should be a callable, and the return value is a Boolean value. Timeout is used to specify the time-out.
This method is equivalent to repeatedly calling wait () until the condition is true or timed out. The return value is the return value of the last predicate, or the time-out returns flase.
Ignoring the timeout attribute, calling this method is equivalent to:
While not predicate (): cv.wait ()
Therefore, calling the method and calling wait () has the same rule: the lock must be acquired first when the call is or is returned from the block.
5) Notify (N=1)
By default, a waiting thread is awakened. If the thread calling the method does not acquire a lock, RuntimeError is thrown.
This method wakes up n (the default is 1) a waiting thread, and if there is no thread waiting, there is no action.
If at least n threads are waiting, the current implementation is just waking N threads. However, it is unsafe to rely on this behavior because some of the future optimizations may wake up to more than n threads.
Note: A wake-up thread is returned from the wait () call only after the request has been requested to the lock. Because notify () does not release the lock, its callers should release the lock.
6) Notify_all ()
Wakes all waiting threads. This method behaves like notify (), but wakes up all waiting threads. If the calling thread does not acquire the lock, the RuntimeError is thrown.
Semaphore Object
This is one of the earliest synchronization primitives in computer science history, invented by Dutch computer scientist Edsger W. Dijkstra (he uses P () and V () instead of acquire () and release ()).
Semaphore manages an internal count minus one per call to acquire (), plus one for each call to release (). The count is not less than 0, and when the acquire () discovery count is 0 o'clock, it is blocked, waiting until other threads call Release ().
Semaphore also supports context management protocols.
Class Threading. Semaphore (value=1)
This class implements the semaphore object. A semaphore manages a count of the number of threads that can enter in parallel. If the count is 0, acquire () blocks until the count is greater than 0. Value defaults to 1.
Value gives the initial value of the internal count, which defaults to 1 and throws ValueError if the value passed in is less than 0.
1) Acquire (Blocking=true, Timeout=none)
Request semaphore.
When there are no parameters: if the internal count is greater than 0, the count is reduced by 1 and returned immediately. If the count is 0, block, wait until another thread calls release (). This is implemented using an interlock mechanism that ensures that if multiple threads call acquire () blocking, release () will only wake up one thread. The wake-up thread is randomly selected, not dependent on the blocking order. Successful return True (or infinite blocking).
If blocking is false, it will not block. If semaphore cannot be obtained, returns false immediately, otherwise, the operation with no arguments, and returns True.
When timeout is set and is not none, it will block up to timeout seconds. Returns False if the semaphore was not successfully obtained within that time, otherwise true.
2) Release ()
Release a semaphore, Count plus 1. If the count is initially 0, you need to wake up a thread in the wait queue.
Class Threading. Boundedsemaphore (value=1)
This class implements a bounded semaphore object. A bounded semaphore will ensure that its current value does not overflow his initial value, and if it overflows, then ValueError is thrown. In most scenarios, semaphore is used to restrict the use of resources. If Semaphore is released too many times, it often indicates that a bug has occurred.
Semaphore Use instances
Semaphore is typically used to control the use of resources, for example, a database server. In some cases, the size of the resource is fixed and you should use a bounded semaphore. Before starting another worker thread, your main thread first initializes the semaphore:
MaxConnections = AA ... pool_sema = Boundedsemaphore (value=maxconnections)
Other worker threads invoke the semaphore request and release method when they need to connect to the server:
With Pool_sema: conn = Connectdb () try: # ... use connection ... Finally: Conn.close ()
Having a bounded semaphore can reduce the chance of a program error and prevent the semaphore from being released more often than the number of times the request was raised.
Event Object
This is one of the simplest communication mechanisms between threads: one thread notifies one event, another thread waits for notification and makes a deal.
An event object manages an internal flag, which can be set to true using the set () method, which can be reset to false using the clear () method. The wait () method blocks until the flag is true.
Class Threading. Event
The class implements the event object. An event management flag that can be set to true through the set () method and reset to False by the clear () method. The wait () method blocks until the flag is true. The flag is initially false.
1) Is_set ()
Returns true if and only if the internal flag is true.
2) Set ()
Set the flag to true. All waiting threads will be awakened. Once the flag is true, the thread calling wait () will no longer block.
3) Clear ()
The reset flag is false. Next, all threads calling Wait () will block until set () is called.
4) Wait (Timeout=none)
Blocks until the flag is set to true. Returns immediately if the flag is already true, otherwise, blocking until another thread calls set (), or until it expires.
When the timeout parameter is specified and is not none, the thread waits only the number of seconds of timeout.
This method returns True when the flag is true when the timeout returns false.
Timer Object
This class represents a timer that indicates how long a behavior is executed. A timer is a subclass of thread that can be used as an instance of creating a custom thread.
The timer starts by calling the start () method, stopping by calling the Cancel () method (which must precede the behavior being executed). The wait time between the timer execution of the specified behavior is not accurate, that is, there may be a difference between the user-specified interval.
For example:
def hello (): print ("Hello, world") T = Timer (30.0, Hello) t.start () # 30 seconds later, "Hello, World" will be printed
Class Threading. Timer (interval, function, Args=none, Kwargs=none)
Create a timer that, after interval seconds, uses the parameters args and Kwargs as parameters to perform the function. If Args is None (the default), an empty list is used. If Kwargs is None (the default), an empty dictionary is used.
1) Cancel ()
Stop the timer and cancel the execution of the timer's behavior. This is only valid if the timer is still processing the wait state.
Barrier Object
The fence provides a simple synchronization primitive for a fixed number of threads that need to wait for each other's scene. Each thread that tries to pass through the fence calls the wait () method, and then blocks until all the threads have called the method, and then all the threads are freed at the same time.
Fences can be reused as many times as possible, but must be of the same number of threads.
Here is an example of a simple way to synchronize clients and service threads:
b = Barrier (2, timeout=5) def server (): start_server () b.wait () while True: connection = Accept_ Connection () process_server_connection (connection) def client (): b.wait () while True: connection = Make_connection () process_client_connection (connection)
Class Threading. Barrier (Parties, Action=none, Timeout=none)
Creates a fence object for parties threads, and if an action is provided, it is called by one of the threads when the thread is freed. Timeout indicates the default time-out value for the Wait () method.
1) Wait (timeout=none)
Through the fence. When all the threads that use the fence call the method, they are released at the same time. If timeout is provided, he takes precedence over the timeout parameter provided by the class constructor.
The return value is an integer from 0 to parties, and each thread is different. This can be used to select a particular thread to do certain things, such as:
i = barrier.wait () if i = = 0: # Only one thread needs to print this print ("passed the Barrier")
If an action is supplied to the constructor, one of the threads will call it when it is released, and if the call throws an exception, the fence goes into a corrupt state.
If the call times out, the fence goes into a corrupt state.
The method throws a Brokenbarriererror exception if the fence is in a damaged state, or if the thread is reset while it is waiting.
2) Reset ()
Restore the fence to the default state. Any thread that is waiting will receive an BROKENBARRIERERROR exception.
Note that additional synchronization is required here. If a fence is damaged, creating a new fence may be a better option.
3) Abort ()
Place the fence into a damaged state. This causes both the currently waiting thread and future calls to wait () to throw brokenbarriererror. This method is typically used to avoid deadlocks.
Using a time-out should be a better choice.
4) parties
The number of threads required to pass through the fence.
5) n_waiting
The number of threads currently in wait.
6) Broken
Whether the fence is in a damaged state, or true if it is.
Exception Threading. Brokenbarriererror
The exception is a subclass of RuntimeError that is thrown when the fence object is reset or damaged.
Using locks, conditions, and semaphores in the WITH statement
All objects with the acquire () and release () methods provided by the module can be managed using the WITH statement. The acquire () method is called when the blocking state is entered, and the release () method is called when the blocking state is rolled out. The following is the specific syntax:
With Some_lock: # do something ...
Equivalent to:
Some_lock.acquire () Try: # do something...finally: some_lock.release ()
Currently, Lock, Rlock, Condition, semaphore, and Boundedsemaphore can all be managed in the WITH statement.
Python multithreaded 1:threading