Python multi-thread programming (1): support for multiple threads in Python

Source: Internet
Author: User
ArticleDirectory
    • Virtual Machine Layer
    • Language Level
    • Third-party support

I have previously introduced the basic concepts of multithreading, understood these basic concepts, and mastered Python multi-threaded programming.

Before getting started, you should first understand Python's support for multithreading.

Virtual Machine Layer

The Python virtual machine uses Gil (Global interpreter lock, global interpreter lock) to mutex the thread's access to shared resources. At present, it cannot take advantage of the multi-processor.

Language Level

At the language level, Python provides excellent support for multithreading. in Python, multithreading-related modules include thread, threading, and queue. It can easily support features such as thread creation, mutex lock, semaphore, and synchronization.

Thread: the underlying support module of multithreading, which is generally not recommended.

Threading: encapsulates threads to visualize operations of some threads and provides the following classes:

Thread class

Timer is similar to thread, but it takes a while to start running.

Lock primitive

Rlock can be reentrant. So that a single thread can obtain the acquired lock again

The condition variable can stop a thread and wait for other threads to meet a certain condition"

The condition variable used by the event. Multiple Threads can wait for an event. After the event occurs, all threads are activated.

Semaphore provides a structure similar to "Waiting Room" for lock-waiting threads

Boundedsemaphore is similar to semaphore, but it cannot exceed the initial value.

Queue: Implements queues of multiple producers and consumers. It supports lock primitives and provides good synchronization support between multiple threads. Provided classes:

Queue queue

Lifoqueue (LIFO) queue

Priorityqueue priority queue

 

The thread class is your main thread class and you can create process instances. The functions provided by this class include:

Getname (Self) returns the thread name

The isalive (Self) Boolean flag indicates whether the thread is still running.

The daemon flag of the thread returned by isdaemon (Self)

Join (self, timeout = none)ProgramSuspended until the thread ends. If timeout is provided, the timeout seconds can be blocked at most.

Run (Self) defines the function of a thread.

Setdaemon (self, daemonic) sets the daemon flag of the thread to daemonic

Setname (self, name) sets the thread name

Start (Self) starts thread execution

 

Third-party support

If you are particularly concerned about performance, you can also consider the implementation of some "micro-threads:

Stackless Python: an enhanced version of Python that provides support for micro-threads. A micro-thread is a lightweight thread. It takes more time to switch between multiple threads and consumes less resources.

Greenlet: a by-product of stackless. It calls microthreads "tasklet ". Tasklet runs in pseudo-concurrency and uses channel for synchronous data exchange. "Greenlet" is a more primitive micro-thread concept without scheduling. You can construct a micro-thread scheduler by yourself, or use greenlet to implement advanced control flow.

In the next section, create and start a thread using python.

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.