1. theading.current_thread() 当前线程对象python
2. .getName() 获取线程名异步
3. .ident() 获取线程idide
4. threading.enumerate() 当前正在运行的线程对象的一个列表函数
5. threading.active_count() 当前正在运行的线程数量spa
1.先进先出队列 queue.Queue()线程
2.先进后出队列 queue.LifoQueue()协程
(和用法相同)对象
3.优先级队列 queue.priorityQueue()blog
Put的数据是一个元组,元组的第一个参数是优先级数字,队列
数字越小优先级越高,越先被get到被取出来,第二个参数
是put进去的值,若是说优先级相同,那么值别忘了应该是
相同的数据类型,字典不行
1.from concurrent_futures import ThreadPoolExecutor,ProcessPoolExecutor
引入模块
2. p = ThreadPoolExecutor(4)
默认的线程个数是cpu个数的5倍
3. p = ProcessPoolExecutor(4)
默认的进程个数是cpu的个数
4. p.map(f1,可迭代对象)
异步执行
5. res = p.submit(f1,11,12)
异步提交任务 参数但是多个
6. res.result()
(和.get方法同样,若是没有结果会等待,阻塞程序)
7. shutdown()
(和close+join用法同样,锁定线程池,等待线程池中的
任务所有执行完毕)
8.线程池的回调函数
协程是一种用户态的轻量级线程,即协程是由用户程序本身控制调度的。
1.生成器类型协程(实现了‘切换+保存状态’,但没有提高效率)
2.greenlet模块类型协程(实现了‘切换+保存状态’,但没有提高效率)
3.gevent模块类型的协程
即实现了‘切换+保存状态’,又提高效率
import gevent from gevent import monkey;monkey.patch_all() import time import threading def f1(): print('第一次f1') # print(threading.current_thread().getName()) # gevent.sleep(1) time.sleep(2) print('第二次f1') def f2(): # print(threading.current_thread().getName()) print('第一次f2') # gevent.sleep(2) time.sleep(2) print('第二次f2') g1 = gevent.spawn(f1) #异步提交了f1任务 g2 = gevent.spawn(f2) #异步提交了f2任务 # g1.join() # g2.join() gevent.joinall([g1,g2]) print('主程序任务')