2019年1月14日

07 线程池回调函数

摘要: import time from threading import current_thread from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor def f1(n,s): return n+s def f2(n): print('回调函数>>>',n.result()) if __n... 阅读全文

posted @ 2019-01-14 15:53 =.=== 阅读(240) 评论(0) 推荐(0)

05 greenlet版协程

摘要: import time # import greenlet from greenlet import greenlet def f1(s): print('第一次f1'+s) g2.switch('taibai') #切换到g2这个对象的任务去执行 time.sleep(1) print('第二次f1'+s) g2.switch() def f2(s):... 阅读全文

posted @ 2019-01-14 15:52 =.=== 阅读(92) 评论(0) 推荐(0)

06 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.... 阅读全文

posted @ 2019-01-14 15:52 =.=== 阅读(89) 评论(0) 推荐(0)

04 生成器版协程

摘要: import time def f1(): for i in range(10): time.sleep(0.5) print('f1>>',i) yield def f2(): g = f1() for i in range(10): time.sleep(0.5) print('f2>... 阅读全文

posted @ 2019-01-14 15:51 =.=== 阅读(106) 评论(0) 推荐(0)

03 线程池

摘要: import time from threading import current_thread from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor def f1(n,s): time.sleep(1) # print('%s号子线程'%current_thread().ident) ... 阅读全文

posted @ 2019-01-14 15:51 =.=== 阅读(71) 评论(0) 推荐(0)

01 线程的其他方法

摘要: import threading import time from threading import Thread,current_thread def f1(n): time.sleep(1) print('子线程名称', current_thread().getName()) #Thread-1 print('%s号线程任务'%n) if __name__ ==... 阅读全文

posted @ 2019-01-14 15:50 =.=== 阅读(85) 评论(0) 推荐(0)

02 线程队列

摘要: import queue # 一:先进先出队列 # q = queue.Queue(3) #先进先出 fifo first in first out # q.put(1) # q.put(2) # # print('当前队列内容长度',q.qsize()) # q.put(3) # print('查看队列是否满了',q.full()) # try: # q.put_nowait... 阅读全文

posted @ 2019-01-14 15:50 =.=== 阅读(124) 评论(0) 推荐(0)

34 线程的其他方法 队列 线程池 协程

摘要: 今日内容: 1 线程的其他方法 2 线程队列 (重点) 3 线程池(重点) 4协程 生成器 Greenlet模块 Gevent模块(重点) 今日内容回顾: 线程的其他方法: Threading.current_thread() #当前线程对象 GetName() 获取线程名 Ident 获取线程id 阅读全文

posted @ 2019-01-14 15:48 =.=== 阅读(143) 评论(0) 推荐(0)

13 事件

摘要: import time from threading import Thread,Semaphore,Event # def func(): # sm.acquire() # print('get sm') # time.sleep(1) # sm.release() # if __name__ == '__main__': # # sm=Semapho... 阅读全文

posted @ 2019-01-14 15:47 =.=== 阅读(86) 评论(0) 推荐(0)

12 子进程不能input

摘要: from threading import Thread from multiprocessing import Process def f1(): name = input('请输入名字') #EOFError: EOF when reading a line # print(name) # if __name__ == '__main__': # inpu... 阅读全文

posted @ 2019-01-14 15:47 =.=== 阅读(213) 评论(0) 推荐(0)

10 gil解释

摘要: import time time.sleep(100) 阅读全文

posted @ 2019-01-14 15:46 =.=== 阅读(135) 评论(0) 推荐(0)

08 多线程的程序不结束多进程的程序不结束的区别

摘要: import time from threading import Thread from multiprocessing import Process #守护进程:主进程代码执行运行结束,守护进程随之结束 #守护线程:守护线程会等待所有非守护线程运行结束才结束 def f1(): time.sleep(2) print('1号线程') def f2(): tim... 阅读全文

posted @ 2019-01-14 15:45 =.=== 阅读(246) 评论(0) 推荐(0)

08 守护线程

摘要: import time from threading import Thread from multiprocessing import Process #守护进程:主进程代码执行运行结束,守护进程随之结束 #守护线程:守护线程会等待所有非守护线程运行结束才结束 def f1(): time.sleep(2) print('1号线程') def f2(): time... 阅读全文

posted @ 2019-01-14 15:45 =.=== 阅读(99) 评论(0) 推荐(0)

07 递归锁

摘要: import time from threading import Thread, Lock, RLock def f1(locA, locB): # print('xxxx') # time.sleep(0.1) locA.acquire() print('f1>>1号抢到了A锁') time.sleep(1) locB.acquire() ... 阅读全文

posted @ 2019-01-14 15:44 =.=== 阅读(99) 评论(0) 推荐(0)

06 死锁现象

摘要: import time from threading import Thread,Lock,RLock def f1(locA,locB): locA.acquire() print('f1>>1号抢到了A锁') time.sleep(1) locB.acquire() print('f1>>1号抢到了B锁') locB.release() ... 阅读全文

posted @ 2019-01-14 15:44 =.=== 阅读(73) 评论(0) 推荐(0)

05 锁 同步 互斥锁

摘要: from multiprocessing import Queue import queue import time from threading import Lock,Thread num = 100 def f1(loc): # num -= 1 loc.acquire() global num # num -= 1 tmp = num t... 阅读全文

posted @ 2019-01-14 15:44 =.=== 阅读(115) 评论(0) 推荐(0)

04 多进程和多线程的效率对比

摘要: import time from threading import Thread from multiprocessing import Process def f1(): # time.sleep(1) #io密集型 # 计算型: n = 10 for i in range(10000000): n = n + i if __name__... 阅读全文

posted @ 2019-01-14 15:43 =.=== 阅读(409) 评论(0) 推荐(0)

02 查看线程的进程id

摘要: import os from threading import Thread # from multiprocessing import Process def f1(n): print('1号',os.getpid()) print('%s号线程任务'%n) def f2(n): print('2号',os.getpid()) print('%s号线程任务'%n... 阅读全文

posted @ 2019-01-14 15:42 =.=== 阅读(217) 评论(0) 推荐(0)

03 验证线程是数据共享的

摘要: import os import time from threading import Thread # from multiprocessing import Process #通过对全局变量的修改来验证线程之间是数据共享的,共享同一进程中的数据 num = 100 def f1(n): # time.sleep(3) global num num = 3 p... 阅读全文

posted @ 2019-01-14 15:42 =.=== 阅读(90) 评论(0) 推荐(0)

01 线程的两种创建方式

摘要: from threading import Thread # from multiprocessing import Process # def f1(n): # print('%s号线程任务'%n) # def f2(n): # print('%s号线程任务'%n) # if __name__ == '__main__': # t1 = Thread(target=f1... 阅读全文

posted @ 2019-01-14 15:41 =.=== 阅读(105) 评论(0) 推荐(0)

33 线程的创建 验证线程之间数据共享 守护线程

摘要: 今日内容 线程理论 什么是线程 线程的两种创建方式(重点) 查看线程的pid 计算密集型和io密集型 线程与进程的效率对比 线程空间不是隔离的 守护线程(**) 锁(重点) 死锁现象(重点) 递归锁(重点) 以后用递归锁 子进程不能input 线程不需要main GIL锁(重点) 今日内容回顾: 线 阅读全文

posted @ 2019-01-14 15:39 =.=== 阅读(87) 评论(0) 推荐(0)

10 进程池的回调函数

摘要: import os from multiprocessing import Pool,Process def f1(n): print('进程池里面的进程id',os.getpid()) print('>>>>',n) return n*n def call_back_func(asdf): print('>>>>>>>>>>>>>',os.getpid())... 阅读全文

posted @ 2019-01-14 15:36 =.=== 阅读(123) 评论(0) 推荐(0)

09 进程池的异步方法

摘要: import time from multiprocessing import Process,Pool def f1(n): time.sleep(0.5) # print(n) return n*n if __name__ == '__main__': pool = Pool(4) res_list = [] for i in ran... 阅读全文

posted @ 2019-01-14 15:35 =.=== 阅读(177) 评论(0) 推荐(0)

07 进程池的同步方法和异步方法

摘要: import time from multiprocessing import Process,Pool def f1(n): time.sleep(0.5) # print(n) return n*n if __name__ == '__main__': pool = Pool(4) # pool.apply(f1,args=(2,)) #同步方法 ... 阅读全文

posted @ 2019-01-14 15:34 =.=== 阅读(194) 评论(0) 推荐(0)

08 进程池同步方法

摘要: import time from multiprocessing import Process,Pool def f1(n): time.sleep(1) # print(n) return n*n if __name__ == '__main__': pool = Pool(4) for i in range(10): prin... 阅读全文

posted @ 2019-01-14 15:34 =.=== 阅读(87) 评论(0) 推荐(0)

05 进程池map方法

摘要: import time from multiprocessing import Process,Pool # def f1(n): # time.sleep(1) # print(n) #对比多进程和进程池的效率 def f1(n): for i in range(5): n = n + i if __name__ == '__main__': ... 阅读全文

posted @ 2019-01-14 15:33 =.=== 阅读(443) 评论(0) 推荐(0)

06 测试多进程的时间

摘要: import time from multiprocessing import Process def f1(): time.sleep(2) print('子进程1号') def f2(): time.sleep(3) print('子进程2号') if __name__ == '__main__': s = time.time() p ... 阅读全文

posted @ 2019-01-14 15:33 =.=== 阅读(104) 评论(0) 推荐(0)

04 信号量

摘要: import time import random from multiprocessing import Process,Semaphore def f1(i,s): s.acquire() print('%s男嘉宾到了'%i) time.sleep(random.randint(1,3)) s.release() if __name__ == '__ma... 阅读全文

posted @ 2019-01-14 15:32 =.=== 阅读(80) 评论(0) 推荐(0)

03 基于事件的进程通信

摘要: import time from multiprocessing import Process,Event def f1(e): time.sleep(2) n = 100 print('子进程计算结果为',n) e.set() if __name__ == '__main__': e = Event() p = Process(targ... 阅读全文

posted @ 2019-01-14 15:32 =.=== 阅读(150) 评论(0) 推荐(0)

02 事件

摘要: from multiprocessing import Process,Event e = Event() #创建事件对象,这个对象的初识状态为False print('e的状态是:',e.is_set()) print('进程运行到这里了') e.set() #将e的状态改为True print('e的状态是:',e.is_set()) e.clear() #将e的状态改为Fals... 阅读全文

posted @ 2019-01-14 15:31 =.=== 阅读(145) 评论(0) 推荐(0)

01 管道

摘要: from multiprocessing import Process,Pipe def f1(conn): from_zhujincheng = conn.recv() print('我是子进程') print('来自主进程的消息:',from_zhujincheng) if __name__ == '__main__': conn1,conn2 =... 阅读全文

posted @ 2019-01-14 15:30 =.=== 阅读(75) 评论(0) 推荐(0)

32 管道 事件 信号量 进程池 线程的创建

摘要: 今日内容 管道:了解 事件:了解 信号量:了解 进程池:(重点) 线程理论 (知道什么是线程,为什么要有线程) 线程的两种创建方式(重点) 今日内容回顾: 管道: Conn1,conn2 = Pipe() Conn1.recv() Conn1.send() 数据接收一次就没有了 事件: E = Ev 阅读全文

posted @ 2019-01-14 15:27 =.=== 阅读(126) 评论(0) 推荐(0)

02 验证进程之间是空间隔离的

摘要: from multiprocessing import Process num = 100 def f1(): global num num = 3 print('子进程中的num',num) print('>>>>>',num) if __name__ == '__main__': p = Process(target=f1,) p.start()... 阅读全文

posted @ 2019-01-14 15:23 =.=== 阅读(73) 评论(0) 推荐(0)

01 进程的其他方法

摘要: import time import os from multiprocessing import Process # def f1(): # print('子进程的pid',os.getpid()) # print('子进程的父进程的pid',os.getppid()) # print('aaa') # # def f2(): # print('bbb') #... 阅读全文

posted @ 2019-01-14 15:23 =.=== 阅读(100) 评论(0) 推荐(0)

4,5 04 互斥锁 抢票程序

摘要: # 互斥锁/进程锁/同步锁 # import json import time from multiprocessing import Process,Lock def show_t(i): with open('ticket','r',encoding='utf-8') as f: ticket_data = f.read() # print(ticket_... 阅读全文

posted @ 2019-01-14 15:22 =.=== 阅读(162) 评论(0) 推荐(0)

03 守护进程

摘要: import time from multiprocessing import Process def f1(): time.sleep(3) print('xxxx') def f2(): time.sleep(5) print('普通子进程的代码') if __name__ == '__main__': p = Process(target=f1,... 阅读全文

posted @ 2019-01-14 15:22 =.=== 阅读(86) 评论(0) 推荐(0)

07 for 循环 创建进程

摘要: import time from multiprocessing import Process def f1(): time.sleep(0.5) print('xxx') if __name__ == '__main__': p_list = [] #for循环创建子进程,并且完成主进程等待所有子进程执行结束,才继续执行 for i in rang... 阅读全文

posted @ 2019-01-14 15:20 =.=== 阅读(159) 评论(0) 推荐(0)

06 数据共享

摘要: import time from multiprocessing import Process,Manager,Lock # a = 10 # # tmp = a # # tmp -= 1 # # a = tmp # a -= 1 # a = a - 1 def f1(m_d,l2): # m_d['num'] -= 1 # with l2: # l2.a... 阅读全文

posted @ 2019-01-14 15:20 =.=== 阅读(79) 评论(0) 推荐(0)

08 队列

摘要: from multiprocessing import Process,Queue q = Queue(3) #创建一个队列对象,队列长度为3,先进先出 q.put(1) # print('>>>>>',q.qsize()) #返回当前队列的内容长度 print(q.full()) q.put(2) # print('>>>>>',q.qsize()) q.put(3) print(q.ful... 阅读全文

posted @ 2019-01-14 15:19 =.=== 阅读(83) 评论(0) 推荐(0)

09 基于队列的进程通信

摘要: from multiprocessing import Process,Queue def f1(q): q.put('约吗?') if __name__ == '__main__': q = Queue(3) p = Process(target=f1,args=(q,)) p.start() son_p_msg = q.get() p... 阅读全文

posted @ 2019-01-14 15:18 =.=== 阅读(71) 评论(0) 推荐(0)

10 通过队列实现一个生产者消费者模型

摘要: import time from multiprocessing import Process,Queue #生产者 def producer(q): for i in range(10): time.sleep(0.7) s = '大包子%s号'%i print(s+'新鲜出炉,拿去用') q.put(s) def c... 阅读全文

posted @ 2019-01-14 15:17 =.=== 阅读(156) 评论(0) 推荐(0)

11 改进版通过队列实现一个生产者消费者模型

摘要: import time from multiprocessing import Process,Queue #生产者 def producer(q): for i in range(10): time.sleep(0.7) s = '大包子%s号'%i print(s+'新鲜出炉,拿去用') q.put(s) def c... 阅读全文

posted @ 2019-01-14 15:16 =.=== 阅读(117) 评论(0) 推荐(0)

13 精进版SVIP版通过队列实现一个生产者消费者模型

摘要: import time from multiprocessing import Process,Queue,JoinableQueue #生产者 def producer(q): for i in range(10): time.sleep(0.2) s = '大包子%s号'%i print(s+'新鲜出炉,拿去用') q.... 阅读全文

posted @ 2019-01-14 15:15 =.=== 阅读(105) 评论(0) 推荐(0)

12 再次改进版通过队列实现一个生产者消费者模型

摘要: import time from multiprocessing import Process,Queue #生产者 def producer(q): for i in range(10): time.sleep(0.2) s = '大包子%s号'%i print(s+'新鲜出炉,拿去用') q.put(s) q.p... 阅读全文

posted @ 2019-01-14 15:15 =.=== 阅读(139) 评论(0) 推荐(0)

31 进程

摘要: 今日内容 操作系统简单介绍 多道技术:(重点) 空间复用 时间复用 进程之间是空间隔离的 分时系统 实时系统 通用操作系统 并发:伪并行,看着像同时运行,其实是任务之间的切换(遇到io切换的会提高代码效率) ,任务切换+保存状态(保存现场) 并行:真正的同时在运行,应用的是多核技术(多个cpu) 进 阅读全文

posted @ 2019-01-14 15:10 =.=== 阅读(116) 评论(0) 推荐(0)

导航