Concurrent programming --- Thread Supplement 2

table of Contents

Thread

event event

Role: to control the execution of the thread,

使用方法:

Event是threading中的一个类,调用里边的一些方法对线程进行一些操作。

e = Event()
在某一个线程中出现了e.wait()的时候,此时这个线程就不能执行,e.wait()可以在多个线程中。
接触e.wait()的方法是,在别的线程中使用e.set(),此时其他线程中的e.wait()的线程都可以继续运行。

例:
from threading import Event
from threading import Thread
import time
# 调用Event类实例化一个对象
e = Event()

# 若该方法出现在任务中,则为False,阻塞
# e.wait()  # False

# 若该方法出现在任务中,则将其他线程的Flase改为True,进入就绪态与运行态
# e.set()  # True


def light():
    print('红灯亮...')
    time.sleep(5)
    # 应该开始发送信号,告诉其他线程准备执行
    e.set()  # 将car中的False ---> True
    print('绿灯亮...')


def car(name):
    print('正在等红灯....')
    # 让所有汽车任务进入阻塞态
    e.wait()  # False
    print(f'{name}正在加速漂移....')


# 让一个light线程任务 控制多个car线程任务
t = Thread(target=light)
t.start()

for line in range(10):
    t = Thread(target=car, args=(f'童子军jason{line}号', ))
    t.start()

Process pool and thread pool

Definition: process thread pool and the pool is used to control the program allows current number of processes / threads.

Question: If unrestricted open process or thread, server cards will collapse.

Role: The role of process thread pool and the pool is created to ensure that the number of threads or processes within the scope of the hardware allows.

use:

# 知识点一:(进程池与线程池的基本使用)
进程池:
from concurrent.futures import ProcessPoolExecutor

ProcessPoolExecutor(5)  # 5代表只能开启5个进程
ProcessPoolExecutor()   # 默认以CPU的个数限制进程数

线程池:
from concurrent.futures import ThreadPoolExecutor

ThreadPoolExecutor(5)  # 5代表只能开启5个进程
ThreadPoolExecutor()   # 默认以CPU个数 * 5限制线程数

# 知识点二:(利用进程池与线程池做的扩展)
pool.submit('传函数地址') # 异步提交任务
相当于下边的两步
t = Thread()
t.start()

# 会让所有线程池的任务结束后,才往下执行代码。
pool.shutdown()

# 知识点三:(回调函数:直接调用函数的返回值)
pool.submit(task, 1).add_done_callback(call_back)
被传函数的返回值,       将函数的返回值传给括号内的回调函数
注意:回调函数一定要写res.result(),因为不许通过res.result()才嫩刚拿到县城任务返回的结果。

# 例:
def task(res):
    print('线程任务开始了...')
    time.sleep(1)
    print('线程任务结束了...')
    return 123


# 回调函数
def call_back(res):
    print(type(res))
    # 注意: 赋值操作不要与接收的res同名
    res2 = res.result()
    print(res2)


for line in range(5):
    pool.submit(task, 1).add_done_callback(call_back)

print('hello')

Coroutine

Process: Resource Unit

Thread: implementation units

Coroutine: In order to achieve concurrent single-threaded, saving resources.

Note: coroutine not the operating system's resources, since he is the program's name, in order to allow single-threaded concurrency.

Coroutine purpose of: manually operating systems Simulation "multi-channel technology", to achieve the state save switching +. In order to keep a single thread to perform switching tasks, so your first task encounters IO operation, and switches to another thread to operate, so you can use one thread requires several threads before you can go to in order to complete complete things, but with coroutines, which will put the thread back and forth between computing tasks, but the efficiency will be lower in the case of single-threaded computing-intensive lines.

Advantage of coroutines: In the case of IO-intensive, will improve efficiency

Coroutine disadvantages: In the case of computationally intensive, switching back and forth, but the efficiency will be lower.

How coroutine: + switching state of preservation.

The use of third-party modules: gevent

Role: to help monitor IO operation, and switches.

Gevent use purpose: to achieve a single thread, the IO encountered realized, to achieve the state save switching +.

from gevent import monkey
monkey.patch_all()  # 可以监听该程序下所有的IO操作
import time
from gevent import spawn, joinall  # 用于做切换 + 保存状态


def func1():
    print('1')
    # IO操作
    time.sleep(1)


def func2():
    print('2')
    time.sleep(3)


def func3():
    print('3')
    time.sleep(5)


start_time = time.time()

s1 = spawn(func1)
s2 = spawn(func2)
s3 = spawn(func3)

s2.join()  # 发送信号,相当于等待自己 (在单线程的情况下)
s1.join()
s3.join()
# 必须传序列类型
joinall([s1, s2, s3])

end_time = time.time()

print(end_time - start_time)

# 输出:
1
2
3
5.011829614639282

Guess you like

Origin www.cnblogs.com/whkzm/p/11735948.html