Python uses threading to implement multi-thread explanation, pythonthreading

Source: Internet
Author: User

Python uses threading to implement multi-thread explanation, pythonthreading

Multithreading

Multithreading is a good way to improve the program running efficiency. The program that originally needs to be executed in sequence can be executed in parallel now, which can be imagined as much more efficient. However, multithreading does not improve the efficiency of all programs. Two extreme program types are 'cpu-intensive 'and' I/O-intensive '. multithreading is more suitable for the latter, in the serial structure, the CPU is idle when you read/write disks or network communication. After all, the network is several orders of magnitude slower than the disk, and the disk is several orders of magnitude slower than the memory, the memory is several orders of magnitude slower than the CPU. Multi-thread technology can be executed at the same time. For example, your program needs to send N http packets (10 seconds) and copy the file from one location to another (20 seconds ), then, you need to count the number of occurrences of the 'hello, world' string in another file (4 seconds). Now it takes 34 seconds in total. However, because these operations are not associated, you can write multi-threaded programs, which takes almost 20 seconds to complete. This is for I/O-intensive instances, but not for CPU-intensive instances. For example, if my program needs to calculate a 1000 factorial (10 seconds) and a 100000 increment (5 seconds), it will take 15 seconds even if the program is in parallel, or even more. When the program uses the CPU, the CPU is rotated to execute, IO-intensive programs can be computed with the CPU at the same time of IO, however, here the CPU-intensive model can only execute thread 1 for a while and then thread 2 for a while. So it takes 15 seconds, or even more, because the CPU needs to be switched over. To solve the multi-thread problem of CPU-intensive programs is a matter of CPU. For example, Intel's hyper-Threading Technology can truly parallel two threads on the same core, so it is called 'dual-core four Threads' or 'quad-core eight Threads'. We will not talk about it here, but I do not know about it.

Python Spoofing

But Python does not support real multi-threaded programming. In Python, there is something called GIL, and Chinese is a global interpreter, which controls Python and allows Python to run only one thread at a time. In the real sense, multithreading is controlled by the CPU, and multithreading in Python is controlled by GIL. If a CPU-intensive program is written in the C language and runs on a quad-core processor, the efficiency can be improved by up to four times if multithreading is used, however, if you use Python to write data, it does not increase or even slows down because of thread switching. Therefore, Python multithreading is more suitable for writing I/O-intensive programs. Besides, C/C ++ is used for CPU-intensive programs with high efficiency requirements.

First Multithreading

The multi-thread library in Python generally uses thread and threading. It is not recommended for beginners and ordinary people to use thread. The threading module is enough.

There is a program, as shown below. Two cycles: Sleep for 3 seconds and sleep for 5 seconds. It takes 8 seconds for serial execution.

#!/usr/bin/env python# coding=utf-8import timedef sleep_3(): time.sleep(3)def sleep_5(): time.sleep(5)if __name__ == '__main__': start_time = time.time() print 'start sleep 3' sleep_3() print 'start sleep 5' sleep_5() end_time = time.time() print str(end_time - start_time) + ' s'

The output is as follows:

start sleep 3start sleep 58.00100016594 s

Then we modify it to make it a multi-threaded program, although there are few changes. First introduce the threading library, and then instantiatethreading.Thread Object. Just upload a function to the constructor. Then, call the start method of Thread to start a Thread.join() The method can wait for the thread to end, as I used below. If I do not add the two code waiting for the end of the thread, the statement of the output time will be executed directly, in this way, the statistical time is incorrect.

#! /Usr/bin/env python # coding = utf-8import timeimport threading # introduce threadingdef sleep_3 (): time. sleep (3) def sleep_5 (): time. sleep (5) if _ name _ = '_ main _': start_time = time. time () print 'start sleep 3' thread_1 = threading. thread (target = sleep_3) # instantiate a Thread object so that the Thread executes this function thread_1.start () # start this Thread print 'start sleep 5' thread_2 = threading. thread (target = sleep_5) # instantiate a Thread object so that the Thread executes this function thread_2.start () # start this Thread thread_1.join () # Wait Until thread_1 ends thread_2.join () # Wait For thread_2 to end end_time = time. time () print str (end_time-start_time) +'s'

The execution result is as follows:

start sleep 3start sleep 55.00099992752 s

Daemon

In our understanding, the daemon thread should be very important, similar to the daemon process in Linux. Howeverthreading.ThreadBut not in.

If you set a thread as a daemon thread, it indicates that this thread is not important. When the process exits, it does not need to wait for the thread to complete its execution. --------- Python core programming Version 3

By default, all threads in the Thread object are non-daemon threads. There are two examples to illustrate the difference. During code execution, the daemon attribute of my_thread is not specified. Therefore, the daemon attribute is not daemon by default, so the process waits for the end of the process. We can see 100 hello, world at last.

#!/usr/bin/env python# coding=utf-8import threadingdef hello_world(): for i in range(100):  print 'hello,world'if __name__ == '__main__': my_thread = threading.Thread(target=hello_world) my_thread.start()

My_thread is set as the daemon thread, so the process exits directly and does not wait for the end of the thread. Therefore, we cannot see 100 hello and there are only a few world threads. Even an exception is thrown to tell us that a thread is not finished.

#! /Usr/bin/env python # coding = utf-8import threadingdef hello_world (): for I in range (100): print 'hello, world 'if _ name _ = '_ main _': my_thread = threading. thread (target = hello_world) my_thread.daemon = True # Set the flag to True my_thread.start ()

PASS Parameters

The previous Code directly executes a piece of code without passing parameters. How can we pass parameters? In fact, it is quite simple.threading.Thread(target=hello_world, args=('hello,', 'world'))You can. Args is followed by a tuple. If there is no parameter, you can leave it empty. If there is a parameter, you can simply add it to the tuple in order.

#! /Usr/bin/env python # coding = utf-8import threadingdef hello_world (str_1, str_2): for I in range (10 ): print str_1 + str_2if _ name _ = '_ main _': my_thread = threading. thread (target = hello_world, args = ('hello, ', 'World') # The parameter my_thread.start () is passed here ()

Another Multithreading

Threading has three methods to create Thread objects, but generally only two methods are used. One is to pass functions mentioned in 0X02, and the other is the inheritance mentioned here.threading.Thread. Here we have defined two classes, which are rewritten in the class. run() Method, that is, callstart() The code to be executed later is the same as the thread to be enabled. The previous method is more process-oriented and more object-oriented.

#!/usr/bin/env python# coding=utf-8import threadingclass MyThreadHello(threading.Thread): def run(self):  for i in range(100):   print 'hello'class MyThreadWorld(threading.Thread): def run(self):  for i in range(100):   print 'world'if __name__ == '__main__': thread_hello = MyThreadHello() thread_world = MyThreadWorld() thread_hello.start() thread_world.start()

Summary

The above is all about this article. I hope this article will help you in your study or work. If you have any questions, please leave a message.

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.