The module was renamed _thread in Python 3.
The module implements a low-level operation primitive for Python multithreading, and also provides a locking mechanism for synchronization between multiple threads. The threading module provides a high-level threading API that is easier to use based on this module. This module is supported on Windows, Linux, SGI IRIX, Solaris 2.x, and platforms that support POSIX thread (a.k.a. "Pthread"), for which thread is not supported The platform of the module, using dummy_thread as an alternative.
Constants and functions defined in the module
exception Thread.error
Thrown when a thread-related exception occurs
thread. LockType
Type of lock Object
thread.start_new_thread (function, args[, Kwargs])
Starts a new thread and returns its identifier.
The newly-started thread invokes functionwith a parameter in the parameter list args (which must be a tuple), and the keyword parameter, kwargs , is optional. When function functions return, the thread that executes it exits silently, and when the function terminates because of an unhandled exception, the thread exits to print the stack trace at the same time.
Thread.interrupt_main ()
Throws an exception keyboardinterrupt in the main thread , which can be used by a child thread to break the main thread
Python 2.3 Introduction
thread.exit ()
Throws an exception systemexit, if the exception is not captured, causes the thread to silently exit
Thread.allocate_lock ()
Returns a new lock object that was not locked at the beginning of the instance
thread.get_ident ()
Returns the thread identifier of the current thread, which is a non-0 integer, and the value is not meaningful, it is used to retrieve the information of the specified thread in a thread-dependent dictionary, and when one of the threads exits, its identifier is recycled waiting to be used when a new thread is created later.
thread.stack_size ([size])
Returns the thread stack size used when creating a thread, and the optional parameter size indicates the stack size when a new thread is created at a later time, either 0 (using the platform default) or an integer that is not less than 32,768 (32kB). If you do not support changing the size of the line stacks, the function throws an exception error. If the specified stack size is not valid, ValueError is thrown and the stack size does not change the next time the new thread is created.
32kB is the minimum stack space currently supported by Python, and using a multiple of 4KB is a more insurance option if the specific platform is not consulted on the size of the thread stack.
Applies to: Windows, systems with POSIX threads.
Python 2.5 Introduction
Lock Object
Lock.acquire ([Waitflag])
When no parameter is called, the method obtains the lock unconditionally, or waits for other threads to release the lock (only one thread can get the lock at a time), and if the integer parameter waitflagis supplied, the behavior of the method depends on this parameter:
If Waitflag is 0: only if the lock can be obtained immediately (without waiting) will the thread get a lock;
If the waitflag is not 0: the thread acquires the lock as if it had no parameters
return value:
Result get lock return True otherwise return False
lock.release ()
The lock must be freed, but it may not be the same thread.
lock.locked ()
Returns the state of the lock object: Returns True if it has been fetched by some thread, otherwise False
Example: using a with statement with a thread lock
Import Threada_lock = Thread.allocate_lock () with A_lock: print "A_lock are locked while this executes"
Precautions
- The interaction between threads and interrupts is strange: Exception Keyboardinterrupt can be received by any thread(when the signal module is available, interrupts are always sent to the main thread
- called sys.exit () or throws an exception Systemexit equivalent to calling thread.exit ()
- The Acquire () method is non-interruptible- keyboardinterrupt occurs after the lock is acquired
- When the main thread exits, the behavior of the other threads is platform-defined, andSGI IRIX is implemented using native threads, so other threads can survive, and on most other platforms, other threads are killed directly without executing the try ... finally clause or other object destructor
- When the main thread exits, it will not be executed except try ... Other than finally , standard I/O will not be brushed out.
Python--thread