# 线程信号量与进程信号量概念一样,允许指定个数的线程访问一段代码 # from threading import Thread # from threading import Semaphore # import time # def func(sem): # sem.acquire() # print('func start...') # time.sleep(1) # print('func end...') # sem.release() # if __name__ == '__main__': # sem = Semaphore(2) # t_lst = [] # for i in range(10): # t = Thread(target=func,args=(sem,)) # t.start() # t_lst.append(t) # for t in t_lst:t.join() ''' func start... func start... func end... func start... func end... func start... func end... func start... func end... func start... func end... func start... func end... func start... func end... func end... func start... func start... func end... func end... ''' # 模拟检测数据库连通性 from threading import Thread from threading import Event import time import random # 数据库连接程序 def checkDB(e): # 模拟数据库连接时间 time.sleep(random.randint(0,3)) e.set() # 模拟数据库连接检测,超时1.5秒则判断数据库连接失败 # def conectDB(e): # count = 0 # while count < 3: # e.wait(0.5) # 状态为Flase,只等待0.5秒 # if e.is_set(): # print('数据库登入成功') # break # else: # count += 1 # else: # # 检测3次1.5秒后,还是False状态,则主动抛出异常,数据库连接失败 # raise TimeoutError('数据库登入失败') # if __name__ == '__main__': # e = Event() # t1 = Thread(target=checkDB,args=(e,)) # t2 = Thread(target=conectDB,args=(e,)) # t1.start() # t2.start() # 线程控制之条件 # from threading import Thread # from threading import Condition # # def fun(con,i): # con.acquire() # con.wait() # print('线程%s开始执行'%(i)) # con.release() # if __name__ == '__main__': # con = Condition() # # 开启10个线程,阻塞在wait处 # for i in range(10): # Thread(target=fun,args=(con,i)).start() # while True: # num = int(input('>>>')) # if num: # con.acquire() # # 跟据给的num(整数)允许指定个数进程非阻塞继续执行 # con.notify(num) # con.release() # 线程控制之定器 # from threading import Timer # import time # def func(): # print('in func') # start_time = time.time() # t = Timer(5,func) # t.start() # t.join() # print(time.time() - start_time) ''' 输出结果:5秒后才开始执行func函数 in func 5.001713514328003 ''' # 自定以定时任务 # from threading import Timer # import time # # def func(): # print('定时执行的代码',time.strftime("%Y/%m/%d %H:%M:%S")) # # if __name__ == '__main__': # while True: # t = Timer(5,func) # t.start() # time.sleep(5) ''' 定时执行的代码 2019/04/16 15:01:30 定时执行的代码 2019/04/16 15:01:35 定时执行的代码 2019/04/16 15:01:40 定时执行的代码 2019/04/16 15:01:45 定时执行的代码 2019/04/16 15:01:50 定时执行的代码 2019/04/16 15:01:55 定时执行的代码 2019/04/16 15:02:00 定时执行的代码 2019/04/16 15:02:05 定时执行的代码 2019/04/16 15:02:10 定时执行的代码 2019/04/16 15:02:15 ''' # queue队列模块 # import queue # 队列--先进后出 # q = queue.Queue(5) # 队列大小为5,put6个数进去会阻塞 # for i in range(6): # q.put(i) # 队列为空再进行get取值也会发生阻塞 # for i in range(5): # q.put(i) # for i in range(6): # print(q.get()) # put_nowait方法当队列已满继续放值,则抛出异常--queue.Full # for i in range(6): # q.put_nowait(i) # get_nowait方法当队列为空再取值,则抛出异常--queue.Empty # for i in range(5): # q.put(i) # for i in range(6): # q.get_nowait() # 队列模块之栈--先进先出 # import queue # q = queue.LifoQueue() # for i in range(3):q.put(i) # for i in range(3):print(q.get()) ''' 2 1 0 ''' # 优先级队列--数值越小,优先级越高,同一优先级值,依据元素自身ascii码表顺序由高到低 import queue q = queue.PriorityQueue() q.put((5,'a')) q.put((2,'b')) q.put((-1,'z')) q.put((-1,'c')) for i in range(4):print(q.get()) ''' (-1, 'c') (-1, 'z') (2, 'b') (5, 'a') '''
Python之信号量,事件,条件与定时器
最新推荐文章于 2024-09-28 11:22:19 发布