Python0016 Multithreading

Python0016 Multithreading


1. Introduction to Multithreading

In modern high-level programming languages, almost all languages ​​support multithreading. Multithreading is practical, even on single-core processors.

In Android development, the Android system forces one thread to handle the graphical interface, and other threads to handle the logic. The advantage of this is that the user graphical interface is smooth. IOS development, the same is true in C# desktop applications. I once wrote Android code to block the UI thread, and the effect on the mobile phone is: the system pops up a prompt box XXXX and does not respond. Usually, similar prompts appear in mobile apps because developers perform a lot of operations in the UI thread, which results in a very poor user experience.

In an image and video processing program based on a multi-core and multi-threaded CPU, due to a large number of operations, multi-threading is usually used for processing, which can achieve the effect of shortening the running time. On a single-core, single-threaded, and one-processor machine, multithreading cannot shorten execution time, but it can also increase execution time.

There are also many problems in the process of using multithreading. Data is shared among multiple threads. At this time, there will be thread safety issues. Suppose a, b threads jointly use the variable sum. In a, sum=0 sum=1+2 print sum is executed, and sum=1 sum=3+4 print sum is executed in b. Since the two threads a and b are executed together, there are multiple execution orders for these six statements. The following two are listed:

(1) normal

a: sum=0

a: sum=1+2

a: print sum #output 3

b: sum=1

b: sum=3+4

b: print sum #output 7

(2) Abnormal

b: sum=1

b: sum=3+4

a: sum=0

a: sum=1+2

b: print sum #output 3

a: print sum #output 3

When this problem occurs, thread locks, thread synchronization mechanisms, etc. are used. Deadlocks can also occur when multiple thread locks are used simultaneously.




2. Multithreading in python

There are two ways to create threads in python:

(1) Write a function to be executed in the child thread and pass it to the Thread class

import threading
import time
#Pass the function to Thread
def print_number(number):
    #The following line is to make the result look more natural, let the thread sleep for 100ms, which can be removed
    time.sleep(0.1)
    print("thread_id:",threading.current_thread().ident,"  num:",number)

threads=[]

#Create 10 threads below
for i in range(10):
    #target The function to be executed args The parameters of the function to be executed Note: (i,) The last parameter must be written like this, otherwise there will be problems, the specific reason has not been explored
    t=threading.Thread(target=print_number,args=(i,))
    threads.append(t)

#start thread
for t in threads:
    t.start()

# A certain run result:
# thread_id: 123145321340928   num: 0
# thread_id: 123145331851264   num: 2
# thread_id: 123145326596096   num: 1
# thread_id: 123145337106432   num: 3
# thread_id: 123145342361600   num: 4
# thread_id: 123145368637440   num: 9
# thread_id: 123145358127104   num: 7
# thread_id: 123145347616768   num: 5
# thread_id: 123145363382272   num: 8
# thread_id: 123145352871936   num: 6


(2) Inherit the Thread class and override the run method

import threading
import time

#Inherit the Thread object and override the run method
class PrintNumThread(threading.Thread):
    def __init__(self,number):
        super(PrintNumThread,self).__init__()
        self.number=number
    def run(self):
        #The following line is to make the result look more natural, let the thread sleep for 100ms, which can be removed
        time.sleep(0.1)
        print("thread_id:",threading.current_thread().ident,"  num:",self.number)

threads=[]

#Create 10 threads below
for i in range(10):
    threads.append(PrintNumThread(i))

#Start these 10 threads
for t in threads:
    t.start()


# A certain run result:
# thread_id: 123145426317312   num: 0
# thread_id: 123145431572480   num: 1
# thread_id: 123145436827648   num: 2
# thread_id: 123145447337984   num: 4
# thread_id: 123145452593152   num: 5
# thread_id: 123145463103488   num: 7
# thread_id: 123145442082816   num: 3
# thread_id: 123145457848320   num: 6
# thread_id: 123145468358656   num: 8
# thread_id: 123145473613824   num: 9

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325721174&siteId=291194637