Python multithreaded 1:threading

Source: Internet
Author: User
Tags semaphore terminates

The threading module provides a high-level thread interface, based on the low-level _thread module implementation.

Basic methods of modules
The methods that are fixed by the module are as follows:
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 control thread for the current thread object, corresponding to the caller.

Assuming that the caller's control thread is not created through the threading module, a limited-functionality virtual thread is returned.
Threading.get_ident ()
Returns the thread identifier of the current thread.

This is a non-0 integer that does not have a specific meaning, and is often used to index thread-specific data dictionaries.

Thread identifiers can be reused.


Threading.enumerate ()
Returns a list of all currently active thread objects. The list contains the sprite thread, the virtual thread object created by Current_thread (), and the main thread.

It does not contain 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 starting from the threading module.

Before each thread's run () method is called. The function will be passed to Sys.settrace () for each thread.


Threading.setprofile (func)
Set up a profile function for all threads starting 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.
Platform top a constant such as the following:
Threading. Timeout_max
The maximum number of timeout parameters for 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 in the following.
The module is designed to emulate the Java threading model. However. Java makes the lock and condition variables the basic behavior of each object. In Python, it is a 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 threads cannot be destroyed, stopped, paused, resumed, or interrupted. When implemented, the static method of the Java thread class is corresponding to the module-level function.
All methods described below are operated 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.


Many other details refer to the _threading_local document string.


Thread Object
The thread class represents a behavior that executes 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. This invokes the run () method in a separate control thread.
Once the thread's behavior is started, the thread is thought to be ' active '.

Normally, when its run () terminates it launches an active state. Or the exception that appears to be handled. The Is_alive () method can be used to test whether the thread is active.
Other threads can invoke the join () method of a thread.

This will block 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 is closed. Their resources (such as open files, database transactions, etc.) cannot be properly released. Assuming 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, such as booting directly from C code. The functionality of Virtual threading objects is limited, and they are always considered to be active sprite threads and cannot be join ().

They cannot be removed because it is impossible to detect the termination of an alien thread.


The following is how the thread class is constructed:


Class threading. Thread (Group=none, Target=none, Name=none, args= (), kwargs={}, *, Daemon=none)  
1) group: should be none, reserved for future expansion, required when a Threadgroup class is implemented;
2) Target: A callable object, called by the Run () method. The tacit feeling of none means that nothing is done.
3) name: The name of the thread, by default, a unique name in the form "Thread-n" is constructed, N is a small decimal number;
4) args: Call the target tuple, default ();
5) Kwargs: The reference dictionary that is called for Target, defaults to {};
6) Daemon: Assuming that it is not none. Sets whether the thread is a sprite thread. If none is assumed, then inherits from the current thread, and
assumes that the subclass overrides the constructor. Before doing anything else, no matter what. It must ensure that the base class's constructor (Thread.__init__ ()) is called first. The regular usage of the
thread is as follows:
1) Start ()
Start the thread.
each thread can only be called at most once. It causes the object's run () method to be called in a separate control thread.


Suppose that more than one call on the same thread object will throw RuntimeError.
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 (the corresponding target parameter), using the corresponding args or kwargs argument.
3) Join (Timeout=none)
Wait until the thread ends. This will block the current thread until the thread that the join () method is called terminates or throws an unhandled exception, or the set overflow time arrives.
Assuming that 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. Because join () always returns NONE, you must call Is_alive () at the end of the join () to infer whether the thread has ended. Assuming that the line Cheng Jinghan is active, the join () call is a time overflow.


When timeout is not specified, or none is specified. The operation will clog until the thread terminates.
A thread can be join () multiple times.
Assuming that an attempt to join the current thread will result in a deadlock, join () will throw runtimeerror.

A join () operation on a thread before it is started will also cause the same exception.
4) name
Thread name, used only to identify a thread, no semantics, multiple threads can be given the same name, the initial name is set by the constructor.


5) GetName ()
SetName ()
Name of the old Getter/setter API. Now replace with the name attribute directly.


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 only returns true after run () is started and before it is terminated. The module function enumerate () returns a list of all active threads.
8) Daemon
A Boolean value. 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 that are created in the daemon default feel false.
When no active non-sprite threads are executing. The entire Python program exits.
9) Isdaemon ()
Setdaemon ()
The old Getter/setter API. This 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 a 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 state is locked, which changes the state of the lock to unlocked and returns immediately. Suppose you try to release 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 determined. Rely on a detailed implementation.
Related classes such as the following:


Class Threading. Lock
This class implements the primitive lock object. The thread is able to request the lock through acquire. Assume that another thread has acquired the lock. The thread is blocked. Until the other thread releases the lock.
1) Acquire (Blocking=true, timeout=-1)
Request a lock, jam or non-clogging.
When the blocking parameter is true (the default), it is blocked until the lock is released. Then acquires the lock and returns TRUE.
When the blocking parameter is false, it will not clog. Assuming that a thread has acquired a lock, the call will return false immediately. Otherwise. Gets the lock and returns TRUE.


When the timeout parameter is greater than 0 o'clock. The maximum number of seconds specified by timeout is blocked.

A timeout of 1 (the default) indicates waiting. When blocking is false, it does not agree to specify a timeout parameter.


Assume that the lock request succeeds. Returns True, otherwise false (such as a timeout) is returned.


2) Release ()
Release a lock. This can be called in whatever thread, not only in the thread that acquires the lock.


When the lock is in the lock state, reset it to unlocked and return. Other blocked threads waiting for the lock will have one thread that can get to the lock.
This method is called 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 a lock, in an unlocked state. No thread owns the lock.
To obtain a lock, a thread calls the acquire () method. Gets the lock and returns; In order to release the lock, a thread calls the release () method. The call to acquire ()/release () can be nested, with only the last release () resetting 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 lock requests must correspond 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 number supported by the current platform.


1) Acquire (Blocking=true, timeout=-1)
Request a lock, jam or non-clogging mode.


When the number of parameters is null: Assume that the thread already has a lock, the recursion level is added one, and then returns. Otherwise, suppose there is a thread that has a lock that is blocked until the lock is released.

If the lock is in an unlocked state (not owned by any thread), the owner thread is set. and set the recursion level to 1, and then return. Assuming that more than one thread is in the blocked 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 parameters is the same, and returns True.


When blocking is false. Will not clog. If the lock is in a locked state, it returns false immediately; And no reference to the scene again, and returns True.
When the timeout parameter is greater than 0 o'clock. Plug up to timeout seconds. If a lock is acquired within timeout seconds, it returns true, otherwise the timeout returns false.
2) Release ()
Release a lock to reduce the recursion level. Assuming that the recursive level is lowered to 0, the state of the lock is reset to unlocked (not owned by any thread). The recursive level is assumed to be greater than 0 if lowered. The lock is still persisted by the caller thread.
This method is called only if 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.

When several condition variables must share the same lock, the incoming is practical. 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 invoke the corresponding method of the associated lock.
Other methods must be called by the bearer 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 waiting threads.
Note: the Notify () and Notify_all () methods do not release the lock, 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 synchronize access to some shared states with a lock, and repeatedly call Wait () for a thread of interest to a particular state. Until they appear to be in a state of interest. The thread that changes this state calls notify () or Notify_all () to notify the waiting thread that the state has changed. Like what. The following is a typical producer-consumer model 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 to see if an entry is available. Because wait () is able to return after waiting for a random time, there is also a possibility that the thread calling notify () does not make the condition true. This problem is always present in multithreaded programming. The Wait_for () method can be used for self-active 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 (). What is used is whether there is one or more waiting threads in the application scenario.

Like what. 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. One condition variable agrees to wait for one or more threads. Until they are notified by a thread.


Assume that 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)
Requests an implicit lock, which invokes the corresponding method of the implied lock. The return value is the return value of the method that suppresses the lock.


2) Release ()
Releases the implicit lock.

This method invokes the corresponding method of the implicit lock. no return value.
3) Wait (Timeout=none)
Wait until it is awakened. or timeout.

Assume that the calling thread does not request a lock. RuntimeError is thrown.
The method releases the implicit lock and then blocks until it is awakened 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 again requests a 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 it is assumed that the thread has requested multiple locks, and 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, there is also an internal interface that is used to restore the recursive level of the lock.


method returns True, assuming that 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 timeout to return flase.


Ignoring the timeout attribute, calling this method is equivalent to:

While not predicate ():    cv.wait ()
So. 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 blockage.
5) Notify (N=1)
By default, a waiting thread is awakened. Assuming that the thread calling the method does not acquire the lock, the RuntimeError is thrown.


This method wakes up n (default 1) waiting threads, assuming no threads are waiting. There is no action.
Assume that at least n threads are waiting. The current implementation is just waking N threads.

However, relying on this behavior is unsafe because some future optimizations may wake up to more than n threads.
Note: A wake-up thread only returns from the wait () call when the request is to the lock. Because notify () does not release the lock, its callers should release the lock.


6) Notify_all ()
Wakes up all waiting threads. This method behaves like notify (), but wakes up all waiting threads. If the calling thread does not acquire a lock, 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. When the acquire () discovery count is 0 o'clock, it is blocked. Wait 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. Assume that the count is 0. The acquire () is blocked until the count is greater than 0.

Value tacitly feels 1.
Value gives the initial value of the internal count, which defaults to 1. If the value passed in is less than 0, the ValueError is thrown.
1) Acquire (Blocking=true, Timeout=none)
Request semaphore.


When there are no parameters: assume that the internal count is greater than 0. Subtract 1 from the count and return immediately. Assume that the count is 0. Jam. Wait until one of the threads has called release (). This is implemented using an interlock mechanism, which guarantees that multiple threads are called acquire () blocking. Then release () will only wake up one thread. The wake-up thread is randomly selected and does not depend on the order of the blockage.

Successful return True (or infinite jam).


Assuming that blocking is false, it will not clog. Suppose you can't get semaphore. Returns false immediately, otherwise. Operation with no parameters, and returns True.
When timeout is set and not none, it will clog up to timeout seconds. If the semaphore is not successfully obtained within that time, false is returned, otherwise true.
2) Release ()
Release a semaphore. Count plus 1. Assuming that the count is initially 0, you need to wake up a thread in the waiting 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. Assume overflow. The ValueError is thrown.

In most scenarios. Semaphore is used to restrict the use of resources.

Assuming that semaphore is released too many times, it often indicates that a bug has occurred.

Semaphore Use instances
Semaphore are often used to control the use of resources. Like what. A database server. In some cases. The size of the resource is fixed and you should use a bounded semaphore.

Before other worker threads are started. Your main thread initializes the semaphore first:

MaxConnections = AA ... pool_sema = Boundedsemaphore (value=maxconnections)
Other worker threads call Semaphore's request and release method when they need to connect to the server:
With Pool_sema:    conn = Connectdb ()    try:        # ... use connection ...    Finally:        Conn.close ()
A bounded semaphore can reduce the chance of a program error and prevent Semaphore from being released more often than the number of requests.
Event Object
This is one of the simplest communication mechanisms between threads: a thread notifies an event, and a 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 will block 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 is blocked 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 that calls wait () will no longer be blocked.
3) Clear ()
The reset flag is false.

Next, all threads calling Wait () will block until set () is called.
4) Wait (Timeout=none)
Block until the flag is set to true. If the flag is already true, it returns immediately; otherwise, it is blocked until one of the threads calls set (), or until it expires.


When the timeout parameter is specified and is not none, the thread waits only 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 to be run. A timer is a subclass of thread that can be used as an instance of creating a thread of its own definition.
The timer starts by calling the start () method, stopping by calling the Cancel () method (which must be before the behavior is run). The wait time between the timer run specified behavior is not accurate, that is, there may be a difference between the user-specified interval.
Like what:
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 the parameters to run the function. Assuming that args is none (the default), an empty list is used. Suppose Kwargs is None (default). The empty dictionary is used.
1) Cancel ()
Stop the timer. and cancels the operation 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.

Every thread that tries to pass through the fence calls the wait () method and then blocks until all the threads call the method. All threads are then freed at the same time.
Fences can be reused multiple times, but must be the same number of threads.


Here is a sample, a simple way to synchronize the client and service thread:

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)
Create a fence object for parties threads, assuming that the action is provided, when the thread is freed. It will be called by one of the threads. Timeout indicates the default time-out value for the Wait () method.
1) Wait (timeout=none)
Through the fence. When all the threads using the fence call the method, they will be released at the same time. Assuming that timeout is provided, he takes precedence over the timeout parameters 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")
Suppose an action is provided to the constructor, and one of the threads will call it when it is released. Suppose the call throws an exception. The fence enters a damaged state.


Assume that the call timed out. The fence enters a damaged state.
Suppose the fence is in a damaged state, or the thread is reset while waiting. Then the method throws a Brokenbarriererror exception.
2) Reset ()
Restore the fence to the default state.

Regardless of what is waiting on the thread will receive the BROKENBARRIERERROR exception.
Note that additional synchronization is required here. If a fence is damaged, creating a new fence may be a better choice.
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
The fence is in a damaged state, assuming true.



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 it enters the blocking state, and the release () method is called when the blocking state is introduced. The following is the detailed 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.