本人一直想做的一个系列
一开始想用All in One
这样洋气的系列名来着
想了下
此系列主要通过最关键的一行 / 几行
代码,快速给出一个问题的解决方案
至于背后的依赖+原理,还请自行自顶向下搜索
t.start()
from threading import Thread
t = Thread(target=my_add_func, args=(1,2))
t.start()
# 阻塞等待结束
ok = t.join()
其中,
def my_add_func(lh, rh):
res = lh + rh
print(res)
return True
ps:
- args 为传入参数组成的元组,target 也只有 func name
- 和 python 返回多值为元组原理相同
扩展功能
res = pool.map(my_func, args_list)
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor() as pool:
res = pool.map(my_func, args_list)
my_func 为单参数
- 还有一种 pool.submit() 方式,单条发射,不单独列出了
q.put();q.get()
from queue import Queue
q = Queue()
q.put(item) # Generator
q.get(item) # Consumer
- 生产者 - 消费者模型
- 队列默认是无限大的
- 只有 get 的阻塞等待功能