python common module (Thread, Lock, Queue, ThreadPool)

The introduction of understanding about the concept of threads and processes:

进程: 打开一个程序至少会有一个进程  它是cpu调度的最小的单位。
线程: 程序执行的最小单位,一个进程里面至少有一个线程,cpu会控制进程里面的线程。

打个比方:(1)打开一个qq就是一个进程的话,那么你可以同时和好多人聊天,和一个人聊天这就是一个线程。
        (2)再打个比方,一条直行的高速公路,分好几个车道,这整个告诉公路就相当于一个进程,
            那些车道就相当于一个个线程,如果有一条车道上的车拐弯,别的车道的车就要等待,不然就撞车了。
注意:
(1)一个cpu同一时间只能处理一件事,如果同时有多个任务,那么就轮换着执行,但是每个任务执行的时间非常短暂,无法感受到。
(2)使用线程的时候,不管它的顺序,因为cpu是随机调度的。
(3)一个程序的执行,就会有一个主线程

Stage one: to implement threads

1. Thread module

Python provides support for threading through two standard libraries thread and threading, threading of the thread is encapsulated. threading module provides Thread, Lock, RLock, Condition other components.

							因此在实际的使用中我们一般都是使用threading

2.Thread class

Here Insert Picture DescriptionHere Insert Picture Description

3. Create a thread

Create threads in python, there are two ways instance of the Thread class inheritance override the Thread class and instance of the Thread class

Examples of the Thread class:
Here Insert Picture Description

4. Create a thread

Thread class inheritance
Here Insert Picture Description

5.Join & setDaemon

(1)

Before talking about these two methods, the main thread needs to know the concepts and sub-threads

The main thread: When a program starts, there is a thread running, the thread is usually called the application's main thread

Child thread: Because the procedure is performed at the outset, if you need to create a thread, the thread that created the child thread the main thread

It reflects the importance of the main thread in two ways: 1. The other is to produce thread child thread 2. Usually it must finalize the implementation of such closed to perform various operations

(2)

join: blocking the caller until a call to join () method of the end of the thread execution, will continue down
Here Insert Picture Description

Phase II: thread communication

1. mutex

In a multi-threaded, all variables for all threads are shared, therefore, to share data between threads biggest danger is that multiple threads simultaneously modify a variable, it would be a mess, so we need a mutex to lock the data .

2. Inter-thread shared global variables

Here Insert Picture Descriptionprompt! Because the threads belong to the same process, so the shared memory region between them. So a global variable is public.

3. there is competition between shared memory problems

from threading import Thread

x = 0
n =1000000
def a(n):
    global x
    for i in range(n):
        x += 1
def b(n):
    global x
    for i in range(n):
        x -= 1

if __name__ == '__main__':
    a = Thread(target=a,args = (n,))
    b = Thread(target=b,args = (n,))
    a.start()
    b.start()
    a.join()
    b.join()
    print(x)

prompt! If one million effect can not occur can continue to add 0 at the back
you'll find strange results! ! !

4. Use locks to control access to shared resources

The following mutex introduced
in multiple threads, all variables for all threads are shared, therefore, to share data between threads biggest danger is that multiple threads simultaneously modify a variable, it would be a mess, so we need a mutex to lock the data.
As long as we operate when the global variable, just before the operation lock, unlock and then after the completion of the operation, to solve the resource contention problems! ! !

Here Insert Picture Description

The basic concept of the queue

An inlet, an outlet a first in first out (FIFO)

Queue operations at a glance:

Into the team: put (item)

A team: get ()

Test empty: empty ()

Full test: full ()

Queue Length: qsize ()

End of the mandate: task_done ()

Wait for completion: join ()

Note:
GET () wait for task completion, if not task_done () indicates that the task is not complete, as long as the sentence was added to indicate completion. Until the end of the execution.
join is blocked until the task is completed (that is, each time taken to complete the standard are task_done () a)

Stage Three: Thread Pool

Concepts pool

The main thread: the equivalent of producers, just submit jobs to the thread pool.

         并不关心线程池是如何执行任务的。    

Thread pools: the equivalent of consumers, is responsible for receiving the task,

       并将任务分配到一个空闲的线程中去执行。         

           因此,并不关心是哪一个线程执行的这个任务。

2. A simple implementation of a thread pool

from threading import Thread
from queue import Queue
import time

class ThreadPool:
	
	def __init__(self,n):
		self.queue = Queue()
		for i in range(n):
			Thread(target = self.worker,daemon = True).start()
	
	def worker(self):
		while True:
			func,args,kwargs = self.queue.get()
			func(*args,*kwargs)
			self.queue.task_done()
	
	def apply_async(self,target,args = (),kwargs = {}):
		self.queue.put((target,args,kwargs))
	
	def join(self):
		self.queue.join()
	
def fun(x):
	print('hello 第%s次'%x)
	time.sleep(3)
	print('帅哥美女就给点赞啦!')

t = ThreadPool(2)
for i in range(10):
	t.apply_async(fun,args = (i,))
t.join()

Built-in thread pool 3.python

from multiprocessing.pool import ThreadPool
import time

pool = ThreadPool(2)       		   #创建两个线程
	
def funa(x,y):
    print('%s好好学习'%x)
    time.sleep(3)
    print('天天向上')

def funb(x,y):
    print('%shello'%x)
    time.sleep(3)
    print('world')

#我们这就是有一个线程池,里面有两个等待处理任务的线程,然后这两个函数就是两个任务,
#线程池里一个线程处理一个,所以会同时输出!如果多于两个任务就会执行等待sleep


pool.apply_async(funa,args = ('我们要————',2))   #将任务添加到线程池
pool.apply_async(funb,args = ('大家要————',4))

pool.close()       					  #close之后则无法向线程池提交任务

#内置线程池,自带守护线程,主线程结束,子线程也跟着结束
#所以需要加阻塞,否则主线程一结束,子线程也跟着结束,无输出
pool.join()          #在join之前可使用终止线程,直接终止线程pool:  pool.terminate()

print('这是程序的最后一行,执行到这里,主线程结束')

4. Other operations pool

Operating a: close - Close Submit channels, not allowed to submit the job

Operating two: terminate - abort the process pool, suspend all tasks

Published 65 original articles · won praise 50 · views 3596

Guess you like

Origin blog.csdn.net/qq_44907926/article/details/104653842