python threading --- thread-based Parallel

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 the  enumerate() 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 using  threading 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 thread current_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 thread  run() before the method is called, FUNC  will be passed to  sys.settrace() .

threading. setprofile (func)

For all the  threading threads start to set the performance test function modules. In each thread  run() before the method is called, FUNC  will be passed to  sys.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 throw  ValueError 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 throw  OverflowError 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.

  1. 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 separate  run() method.

  2. 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.

  3. Other threads can call a thread  join() method. This method will block the calling thread until being called  join() to the thread end.

  4. Thread has a name. Name can be passed to the constructor, you can also  name read or modify the properties.

  5. 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.

Guess you like

Origin www.cnblogs.com/TianLiang-2000/p/11708084.html