threading
--- Based on parallel threads
This module module lower level of _thread
the establishment of higher thread-interface basis.
This module defines the following functions:
-
threading.
active_count
() -
Returns the survival of the current thread class
Thread
object. Returns the count is equal to theenumerate()
length of the list is returned.
-
threading.
current_thread
() -
Returns the current corresponding to the caller's thread of control
Thread
objects. If the caller's thread of control instead of usingthreading
creation will return a dummy thread object with limited functionality.
-
threading.
get_ident
() -
Returns the current thread "thread identifier." It is a non-zero integer. Its value has no direct meaning, is mainly used as a magic cookie, such as an index containing the thread-related data dictionary. Thread identifier may be in the thread exits, be reused when a new thread is created.
The new 3.3 features.
-
threading.
enumerate
() -
Returns the current all living in a list
Thread
object. The list contains the daemon threadcurrent_thread()
created virtual objects and thread the main thread. It does not contain the end of the thread and the thread has not yet started.
-
threading.
main_thread
() -
Return to the main
Thread
object. Under normal circumstances, the main thread is the thread created at the start of the Python interpreter.The new 3.4 features.
-
threading.
settrace
(func) -
All
threading
threads started tracking function module settings. In each threadrun()
before the method is called, FUNC will be passed tosys.settrace()
.
-
threading.
setprofile
(func) -
For all the
threading
threads start to set the performance test function modules. In each threadrun()
before the method is called, FUNC will be passed tosys.setprofile()
.
-
threading.
stack_size
([size]) -
Stack size used when creating a thread return. Optional parameters size stack size after the specified new thread, but it must be 0 (or a default configuration of the internet), or the minimum is 32,768 (32KiB) is a positive integer. If size is not specified, the default is 0. If it does not change the thread stack size, it will throw
RuntimeError
an error. If the specified stack size is not legitimate, it will throwValueError
an error and does not modify the stack size. 32KiB minimum current to ensure sufficient size interpreter stack has stack space. Note that part of the platform-specific limitation on the size of the stack have, for example, requires that the stack size is greater than or require multiple 32KiB The entire system memory allocated pages - internet documents for more information should be consulted (page 4KiB relatively common, no more in the case of specific information, the recommended approach is to use 4096 as a multiple of the size of the stack).Applies to : Windows, POSIX threads have a system.
This module also defines the following constants:
-
threading.
TIMEOUT_MAX
-
Blocking function (
Lock.acquire()
,RLock.acquire()
,Condition.wait()
, ...) the parameter timeout maximum allowed. Incoming exceed this level timeout will throwOverflowError
an exception.The new 3.2 features.
This module defines a number of classes, described in the following section.
The module is designed based on Java threading model. However, in Java, locks and condition variables are the basic features of each object, which in Python, these are separate objects became independent. Python's Thread
class is just a subset of the Java Thread class; there are no priorities, no thread group, the thread can not yet be destroyed, stop, pause, resume or interrupted. Java static method of the Thread class in the realization mapped to the module-level function.
The method described in the following are performed automatically.
-
But when a thread object is created, it will be due to the activities of the calling thread
start()
start method. This thread of control calls in a separaterun()
method. -
Once the thread activity began, the thread is considered 'alive'. When it's
run()
method ended (either normal or thrown unprocessed), it is not 'survive'.is_alive()
The method used to check whether the thread is alive. -
Other threads can call a thread
join()
method. This method will block the calling thread until being calledjoin()
to the thread end. -
Thread has a name. Name can be passed to the constructor, you can also
name
read or modify the properties. -
A thread can be marked as a "guardian of the thread." The significance of this flag is the only daemon threads are ended, the entire Python program will quit. The initial value of the inheritance to create a thread. This flag can be
daemon
characterized in properties or guardian constructor parameter set.
Notes: daemon thread will suddenly shut down when the program is closed. Their resources (for example, has been an open document, database transactions, etc.) may not be properly released. If you want to stop your normal thread, set them to be non-daemon mode and use appropriate signaling mechanism
6. There is a "main thread" subject; Python program which corresponds to this initial thread of control. It is not a daemon thread.
7. "virtual thread object" can be created. These are corresponding to "external thread" thread objects, which are outside the thread start thread of control modules, for example, directly from the C code. Virtual thread object with limited functionality; they are always considered to be viable and guard mode, can not be join()
. Because they can not detect the end of foreign thread, they will never be deleted.