# import threading #导入线程模块 # def func(): # print("越努力越幸运") # for i in range(10): # func_thread=threading.Thread(target=func) #为func执行函数创建线程 # func_thread.start() #启动线程 # import threading # import time # def sing(): # for i in range(5): # print("唱歌中") # time.sleep(0.1) # def dance(): # for i in range(5): # print("跳舞中") # time.sleep(0.1) # if __name__ == '__main__': # #获取当前线程对象,判断代码是哪个线程执行的 # # print(threading.current_thread()) # #查看程序活动线程列表 # # thread_list=threading.enumerate() # # print("1==",thread_list,len(thread_list)) # #获取活动线程的数量 # print("1==",threading.active_count()) # #创建线程 # sing_thread=threading.Thread(target=sing) # dance_thread=threading.Thread(target=dance) # #查看程序活动线程列表 # # thread_list=threading.enumerate() # # print("2==",thread_list,len(thread_list)) # #启动线程 # sing_thread.start() # dance_thread.start() # #查看程序活动线程列表 # # thread_list=threading.enumerate() # # print("3==",thread_list,len(thread_list)) """线程等待""" # import threading # import time # def sing(): # for i in range(5): # print("唱歌中...") # time.sleep(0.1) # def dance(): # for i in range(5): # print("跳舞中...") # time.sleep(0.1) # if __name__ == '__main__': # print("主线程开始执行...") # #创建子线程 # sing_thread=threading.Thread(target=sing) # dance_thread=threading.Thread(target=dance) # #启动子线程 # sing_thread.start() # dance_thread.start() # #设置等待 # sing_thread.join() # dance_thread.join() # print("主线程结束执行...") # import threading # import time # def sing(num): # for i in range(num): # print("唱歌中...") # time.sleep(0.1) # def dance(num): # for i in range(num): # print("跳舞中...") # time.sleep(0.1) # if __name__ == '__main__': # print("主线程开始执行...") # #创建子线程 # sing_thread=threading.Thread(target=sing,args=(10,)) # dance_thread=threading.Thread(target=dance,args=(10,)) # #设置守护主线程 # sing_thread.setDaemon(True) # dance_thread.setDaemon(True) # #启动子线程 # sing_thread.start() # dance_thread.start() # print("主线程结束执行...") """多线程共享全局变量""" # import threading # alist=[11,22] # def work1(): # alist.append(33) # print("work1:",alist) # def work2(): # print("work2:",alist) # if __name__ == '__main__': # #创建子线程 # work1_thread=threading.Thread(target=work1) # work2_thread=threading.Thread(target=work2) # #启动线程 # work1_thread.start() # work2_thread.start() # """global全局变量 在多线程中可以修改全局变量""" # # import threading # # a=100 # # def work1(): # # global a # # for i in range(3): # # a+=1 # # print(a) # # def work2(): # # print(a) # # if __name__ == '__main__': # # work1_thread=threading.Thread(target=work1) # # work2_thread=threading.Thread(target=work2) # # work1_thread.start() # # work2_thread.start() """多线程间共享全局变量的问题 提高代码复用率;恶意竞争资源,导致计算结果错乱 """ # import threading # import time # num=0 # def work1(): # global num # for i in range(10000000): # num+=1 # print("work1:",num) # def work2(): # global num # for i in range(10000000): # num+=1 # print("work2:",num) # if __name__ == '__main__': # #创建子线程 # work1_thread=threading.Thread(target=work1) # work2_thread=threading.Thread(target=work2) # #启动 # work1_thread.start() # work1_thread.join() # time.sleep(1) #不推荐 # work2_thread.start() """互斥锁""" # import threading # import time # num=0 # lock=threading.Lock() #创建锁 # def work1(): # lock.acquire() #锁定 # global num # for i in range(10000000): # num+=1 # print("work1:",num) # lock.release() #释放 # def work2(): # lock.acquire() # global num # for i in range(10000000): # num+=1 # print("work2:",num) # lock.release() # if __name__ == '__main__': # #创建子线程 # work1_thread=threading.Thread(target=work1) # work2_thread=threading.Thread(target=work2) # #启动 # work1_thread.start() # work1_thread.join() # time.sleep(1) #不推荐 # work2_thread.start() """死锁""" # import threading # lock=threading.Lock() #创建锁 # my_list=[1,2,3,4,5] # def get_data(index): # lock.acquire() #枷锁 # if index >= len(my_list): # print("索引越界:%s"%index) # print("列表已被掏空") # # lock.release() # else: # value=my_list[index] # print("列表中拿出的数据为:%s"%value) # lock.release() #释放锁 # # if __name__ == '__main__': # for i in range(10): # #创建线程 # get_data_thread=threading.Thread(target=get_data,args=(i,)) # #启动线程 # get_data_thread.start() """创建进程""" # import multiprocessing # import time # def func(): # for i in range(10): # print("666",i) # time.sleep(0.5) # if __name__ == '__main__': # f_process=multiprocessing.Process(target=func) # f_process.start() """查看进程号""" # import multiprocessing # import time,os # def func(): # print("pid:-1-",os.getpid()) # for i in range(3): # print("666",i) # time.sleep(0.5) # if __name__ == '__main__': # print("pid:-2-",os.getpid()) # f_process=multiprocessing.Process(target=func) # f_process.start() # time.sleep(3) # print(f_process.is_alive()) """创建多个子进程""" # import multiprocessing # import time # def work1(): # for i in range(10): # print("张傲请吃屎",i+1) # time.sleep(0.3) # def work2(): # for i in range(10): # print("张傲请吃饭",i+1) # time.sleep(0.3) # if __name__ == '__main__': # work1_process=multiprocessing.Process(target=work1) # work2_process=multiprocessing.Process(target=work2) # work2_process.start() # work2_process.join(1) # work1_process.start() # import multiprocessing # import time # def work1(): # for i in range(10): # print("张傲请吃屎",i+1) # time.sleep(0.3) # def work2(): # for i in range(10): # print("张傲请吃饭",i+1) # time.sleep(0.3) # if __name__ == '__main__': # print("主进程开始执行") # work1_process=multiprocessing.Process(target=work1) # work2_process=multiprocessing.Process(target=work2) # work2_process.start() # work2_process.join(1) # work1_process.start() # print("主进程结束执行") # work1_process.terminate() # work2_process.terminate() """多进程间不共享全局变量""" # import multiprocessing # my_list=[11,22] # def write_data(): # my_list.append(33) # print(my_list) # def read_data(): # print(my_list) # # if __name__ == '__main__': # write_process=multiprocessing.Process(target=write_data) # read_process=multiprocessing.Process(target=read_data) # write_process.start() # read_process.start() """进程间通信""" # import multiprocessing # import time # def write_data(queue): # for i in range(10): # if queue.full(): # print("队列已满") # break # else: # queue.put(i) # print("+",i) # def read_data(queue): # while True: # if queue.empty(): # print("队列已空") # break # else: # ret=queue.get() # print("-",ret) # if __name__ == '__main__': # # 创建队列最大容量为5 # queue=multiprocessing.Queue(5) # #创建进程 # write_process=multiprocessing.Process(target=write_data,args=(queue,)) # read_process=multiprocessing.Process(target=read_data,args=(queue,)) # #启动 # write_process.start() # write_process.join() # read_process.start() """线程:1--52数字;a--z字符""" # import threading,time # def num(): # for i in range(1,53): # print(i) # time.sleep(0.2) # def str(): # ret=[chr(i) for i in range(ord("a"),ord("z")+1)] # print(ret) # time.sleep(0.2) # if __name__ == '__main__': # num_thread=threading.Thread(target=num) # str_thread=threading.Thread(target=str) # num_thread.start() # num_thread.join() # str_thread.start() """守护主进程或销毁子进程""" # import multiprocessing # import time # def work(): # for i in range(10): # print("工作中") # time.sleep(0.3) # if __name__ == '__main__': # work_process=multiprocessing.Process(target=work) # # work_process.daemon=True #守护主进程,主进程退出后子进程直接销毁 # work_process.start() # time.sleep(1) # print("over") # work_process.terminate() #销毁子进程 """进程池""" # import multiprocessing # import time,os # def copy_work(): # print(os.getpid()) # print("正在复制中") # time.sleep(5) # if __name__ == '__main__': # #创建进程池 # #指定进程池数量表示进程池中最多有指定的数列 # pool=multiprocessing.Pool(3) # for i in range(10): # #让进程池执行复制任务 # #使用同步的方式去执行任务,进程池中的进程需要等待其他进程执行完成以后才能执行指定的任务 # pool.apply(copy_work) # #异步执行(本质类似于--多线程并行操作),进程池中的进程一起执行,不会等待其他进程的执行 # pool.apply_async(copy_work) # #提示:主进程不会等待进程池把任务执行完成以后程序退出 # #提示:进程池不再接受其他需要执行的任务 # pool.close() # pool.join() # pool.terminate() # import multiprocessing # import time,os # def copy_work(): # print(os.getpid()) #查看进程号 # print("666666") # time.sleep(5) # if __name__ == '__main__': # pool=multiprocessing.Pool(3) #创建进程池 # for i in range(10): # pool.apply(copy_work) #同步执行 # pool.apply_async(copy_work) #异步执行 # pool.close() #关闭pool # pool.join() #等待子进程的退出 # pool.terminate() #不管任务是否完成,立即结束执行 """ 10--借助线程队列模拟演示生产者消费者模式 """ # import threading,queue # import time # def write_data(q): # 生产者 # for i in range(10): # if q.full(): # break # else: # q.put(i) # print(i,"被生产出来") # time.sleep(2) # def read_data(q): # 消费者 # while True: # if q.qsize() == 0: # break # else: # data = q.get() # print(data,"被消费掉") # time.sleep(2.02) # if __name__ == '__main__': # # 创建线程队列 # q = queue.Queue() # # 创建线程 # write_data_thread = threading.Thread(target=write_data,args=(q,)) # read_data_thread = threading.Thread(target=read_data,args=(q,)) # # 启动线程 # write_data_thread.start() # read_data_thread.start() """ 11--使用进程池-队列模拟演示进程间通信 """ # 11题答案 # import multiprocessing # import time # # # 向进程池中的queue写入数据 # def write_data(queue): # for i in range(10): # queue.put(i) # print("写入数据--",i) # time.sleep(0.5) # # # # 向进程池中的queue读取数据 # def read_data(queue): # while True: # if queue.empty(): # break # else: # data = queue.get() # print("读取数据--",data) # time.sleep(0.5) # # if __name__ == '__main__': # # 创建进程池队列 # queue = multiprocessing.Manager().Queue() # # 创建进程池 # pool = multiprocessing.Pool(2) # # 启动进程池方式01 # pool.apply(write_data, args=(queue,)) # pool.apply(read_data, args=(queue,)) # # 启动进程池方式02(混乱) # # pool.apply_async(write_data,args=(queue,)) # # pool.apply_async(read_data,args=(queue,)) # # 启动进程池方式03(解决方式02(混乱)的问题) # # ret = pool.apply_async(write_data,args=(queue,)) # # ret.wait() # # pool.apply_async(read_data,args=(queue,)) # # #(三种方法建议学会第一种和第三种) # pool.close() # pool.join()
"""协程演示::内部封装yield关键字""" # import time # def work1(): # while True: # print("work1...") # time.sleep(0.1) # yield # def work2(): # while True: # print("work2...") # time.sleep(0.1) # yield # if __name__ == '__main__': # g1=work1() # g2=work2() # while True: #使用无限循环唤醒 # next(g1) # next(g2) """greenlet模块""" # import greenlet # import time # def work1(): # for i in range(5): # print("work1...") # time.sleep(0.5) # g2.switch() # def work2(): # for i in range(5): # print("work2...") # time.sleep(0.5) # g1.switch() # if __name__ == '__main__': # #创建协程 # g1=greenlet.greenlet(work1) # g2=greenlet.greenlet(work2) # #启动协程 # g1.switch() """gevent模块(异步IO)是协程中最高级的模块""" # import gevent # import time # from gevent import monkey #打补丁找猴子 # monkey.patch_all() #猴子.匹配全部 # def work1(): # for i in range(10): # print("work1...") # time.sleep(0.2) # def work2(): # for i in range(10): # print("work2...") # time.sleep(0.2) # if __name__ == '__main__': # #创建协程 # g1=gevent.spawn(work1) # g2=gevent.spawn(work2) # #启动,并设置等待 # g1.join() # g2.join() """扩展gevent""" # import gevent # import time # def work1(): # for i in range(10): # print("work1...") # gevent.sleep(0.2) # def work2(): # for i in range(10): # print("work2...") # gevent.sleep(0.2) # if __name__ == '__main__': # #创建协程 # g1=gevent.spawn(work1) # g2=gevent.spawn(work2) # #启动,并设置等待 # # g1.join() # # g2.join() # gevent.joinall([g1,g2])
posted on 2019-05-30 20:54 阅读( ...) 评论( ...)