Implementation of multi-thread and threading in Python-Python tutorial

Source: Internet
Author: User
This article mainly introduces the implementation of multi-thread and threading in Python. it is an important application. anyone who needs to learn Python should know that Python supports multiple threads, it is also a native thread. This article mainly uses the thread and threading modules to implement multithreading.

The thread module of python is a relatively low-level module. the threading module of python is encapsulated for thread and can be used more conveniently.

Here, we need to mention that python does not fully support threads and cannot use multiple CPUs. However, we have considered improving this in the next version of python. let's wait and see.

The threading module is mainly used to visualize some Thread operations and creates a class called Thread.

Generally, there are two ways to use a Thread. one is to create a function to be executed by the Thread, and pass the function into the Thread object for execution; the other is to inherit from the Thread directly, create a new class, and put the code executed by the Thread into this new class.

Let's take a look at these two methods.

I. multi-thread implementation using Python thread

#-*-Encoding: gb2312-*-import string, threading, time def thread_main (a): global count, mutex # obtain the thread name threadname = threading. currentThread (). getName () for x in xrange (0, int (a): # obtain the lock mutex. acquire () count = count + 1 # release the lock mutex. release () print threadname, x, count time. sleep (1) def main (num): global count, mutex threads = [] count = 1 # Create a lock mutex = threading. lock () # first create a thread object for x in xrange (0, num): threads. append (threading. thread (target = thread_main, args = (10,) # Start all threads for t in threads: t. start () # wait for all sub-threads to exit in the main thread for t in threads: t. join () if _ name _ = '_ main _': num = 4 # create four threads main (4)

II. multi-thread implementation using Python threading

#-*-Encoding: gb2312-*-import threadingimport time class Test (threading. thread): def _ init _ (self, num): threading. thread. _ init _ (self) self. _ run_num = num def run (self): global count, mutex threadname = threading. currentThread (). getName () for x in xrange (0, int (self. _ run_num): mutex. acquire () count = count + 1 mutex. release () print threadname, x, count time. sleep (1) if _ name _ = '_ main _': global count, mutex threads = [] num = 4 count = 1 # Create a lock mutex = threading. lock () # create a thread object for x in xrange (0, num): threads. append (Test (10) # start the thread for t in threads: t. start () # wait until the sub-thread ends for t in threads: t. join ()

I believe that the Python multi-threaded instance described in this article can provide some reference for everyone's Python program design.

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.