Python multithreaded 4:_thread

Source: Internet
Author: User
Tags posix stack trace

The _thread module is the base module of the threading module, providing a low level of primitives (also known as lightweight processes or tasks) for multithreading. In order to synchronize, a simple lock (also known as a mutex or a two-dollar semaphore) is provided. The threading module provides an easier-to-use and higher-level threading API based on this module.

The module supports Windows, Linux, SGI IRIX, Solaris 2.x, and any system that implements POSIX threading.

_thread Module
It defines the following constants and functions:


exception _thread.error
Thread error. Starting with the 3.3 version is equivalent to RuntimeError.

_thread. LockType
The lock object type.

_thread.start_new_thread (function, args[, Kwargs])
Begins a new thread and returns its identifier. The thread uses the parameter args (which must be a tuple) to execute the function functions. Optional parameter Kwargs A dictionary of parameters is developed. When the function returns, the thread exits. When the function terminates abnormally, a stack trace is printed and the thread exits (but the other threads continue to execute).

_thread.interrupt_main ()
Throws a Keyboardinterrupt exception on the main thread. A child thread can use this function to interrupt the main thread.

_thread.exit ()
Throws a Systemexit exception. If it is not captured, it causes the thread to exit.

_thread.allocate_lock ()
Returns a new lock object. The lock method is described in the next section. The lock is initially in an unlocked state.

_thread.get_ident ()
Returns the thread identifier of the current thread. This is a non-0 integer. Its value has no direct meaning; he is used as a magic cookie, for example, to index dictionary information for specific thread data. Thread identifiers are used in loops.

_thread.stack_size ([size])
Returns the size of the line stacks, which is used when creating a new thread. Optional parameter size specifies the stack size required to create the thread next, must be 0 (with platform or configuration default) or a positive integer of minimum 32768 (32KiB), if changing the thread stack size is not supported, a RuntimeError exception is thrown, and if the specified stack size is not valid, A valueerror is thrown and the stack size is not modified. 32KiB is the smallest stack size currently supported, guaranteeing enough stack space for the interpreter itself. Note that some platforms have special restrictions on the value of the stack size, such as requiring a minimum stack size greater than 32KiB, or requiring that a multiple of the system memory page size be allocated (refer to the Platform document, the page size is usually 4KiB). Available on the following platforms: Windows, systems with POSIX threads.

_thread. Timeout_max
The maximum value allowed for the timeout parameter of Lock.acquire (). If the specified value exceeds this value, Overflowerror is thrown.
Lock Object
The lock object contains the following methods.


Lock.acquire (waitflag=1, Timeout=-1)
Without parameters, the method requests the lock unconditionally, and if necessary, the thread waits until the other thread releases the lock (only one thread can acquire the lock at a time).
If the integer parameter Waitflag is set, the behavior depends on the value of the parameter: if 0, the lock will not wait for an immediate return, and if it is not 0, the lock waits unconditionally.
If the floating-point parameter timeout is set and is greater than 0, it specifies the maximum number of seconds to wait. A negative timeout indicates a permanent wait. If Waitflag is 0, you cannot specify timeout.
Returns true if the lock gets successful, otherwise returns false.
Starting with version 3.2, acquire can be interrupted by POSIX signals.

lock.release ()
To release a lock, the lock must first be fetched, but not the same thread.

lock.locked ()
Returns the state of a lock: Returns True if it has already been fetched by a thread, otherwise false.

For these methods, you can also use the WITH statement, for example:
Import _threada_lock = _thread.allocate_lock () with A_lock:    print ("A_lock are locked while this executes")

Python multithreaded 4:_thread

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.