Python并发编程06 /阻塞.异步调用/同步调用.异步回调函数.线程queue.事件event.协程 目录 Python并发编程06 /阻塞.异步调用/同步调用.异步回调函数.线程queue.事件event.协程 1. 阻塞 2. 异步调用.同步调用 1. 概念 2. 异步调用 3. 同步调用 3. 异步调用+回调函数 4. 线程queue 5. 事件event 6. 协程 1. 阻塞 进程运行的三个状态:运行,就绪,阻塞 阻塞非阻塞是从执行任务的角度来看的: 阻塞:程序运行时,遇到了IO,…
day38 线程queue 多线程抢占资源 只能让其串行--用到互斥锁 线程queue 队列--先进先出(FIFO) import queue q = queue.Queue(3) q.put(1) q.put(2) q.put(3) # q.put(4) # 阻塞等其他进程或者线程来拿 print(q.get()) print(q.get()) print(q.get()) # print(q.get(block=False)) # 没有值就直接报错 # q.get(timeout=2) #…
1. 线程的一些其他方法 threading.current_thread()  # 线程对象 threading.current_thread().getName()  # 线程名称 threading.current_thread().ident   # 当前线程ID threading.get_ident()  #  当前线程ID threading.enumerate()  # 连同主线程在内的正在运行的线程名称 threading.active_count()  # 活跃的线程数 fr…
# from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor # import requests # import os # import time # import random # # def get(url): # print('%s GET %s' %(os.getpid(),url)) # response=requests.get(url) # time.sleep(random.randint(1,3…
一.线程队列 队列:1.Queue 先进先出 自带锁 数据安全 from queue import Queue from multiprocessing import Queue (IPC队列)2.LifoQueue后进先出 后进先出 自带锁 数据安全 from queue import LifoQueue lq=LifoQueue(5) lq.put(123) lq.put(666) lq.put(888) lq.put(999) lq.put("love") print(lq.pu…
1.线程的其他方法 from threading import Thread,current_thread import time import threading def f1(n): time.sleep(1) print('子线程名称',current_thread().getName())#获取线程名 if __name__=='__main__': t1=Thread(target=f1,args=(1,)) t1.start() print('主线程名称',current_threa…
1 线程的其他方法 threading.current_thread().getName()    查询当前线程对象的名字 threading.current_thread().ident             查询当前进程对象的ID threading.enumerate()                            目前正在活动中的线程 threading.active_count()                         目前有几条活动中的线程 2 线程队列 (数据…
9.14 线程Event connect线程执行到event.wait()时开始等待,直到check线程执行event.set()后立即继续线程connect from threading import Event,current_thread,Thread import time event=Event() def check(): print('%s 正在检测服务是否正常....' %current_thread().name) time.sleep(3) event.set() ​ def…
我们都知道使用gevent协程时,经常会看见在导入包的时候看见这样的代码 from gevent import monkey; monkey.patch_all() monkey.patch_all()作用呢,就是将阻塞的改成非阻塞 具体有哪些呢. Example:  执行发现报错了.loop_exit,线程异常退出.线程改成非阻塞失败.(__此问题摘自遇到此问题的一个微友) import random, datetime, time, json import gevent from geven…
Table of Contents 前言 协程 async & await 事件循环 asyncio 的事件循环 结语 参考链接 前言 Python 标准库 asyncio 是我目前接触过的最难理解的 Python 库,它的实现使用了太多我不太熟悉的东西:协程.事件循环.多路 I/O 复用-- 我对这些概念的认识基本都是停留在知道有这么个东西,大概有什么用上,当真的遇到这些东西的使用的时候,就抓了瞎. 然而,运气很好的是,随着协程使用的普及(不只是 Python),现在可以在网上找到很多相关的文…