Python3之网络编程总结
来源:互联网 发布:淘宝店铺租金 编辑:程序博客网 时间:2024/06/05 17:48
重中之重
进程池、线程池
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutorimport timedef task(n): time.sleep(2) return n ** 2def deal_result(ret): # print(ret) ret = ret.result() print(ret)if __name__ == '__main__': # p = ProcessPoolExecutor() p = ThreadPoolExecutor() # 默认20线程 p_list = [] # obj = p.map(task, range(10)) # print(list(obj)) for i in range(10): obj = p.submit(task, i).add_done_callback(deal_result) p_list.append(obj) p.shutdown() print([i.result() for i in p_list])
生产者消费者模型
进程实现消费者生产者模型
from multiprocessing import Process, JoinableQueueimport timeimport multiprocessingq = JoinableQueue()def consumer(q): while 1: res = q.get() time.sleep(2) print('消费者消费了%s' % res) q.task_done()def producer0(q): for i in range(5): q.put(i) print('生产者生产了%s' % i) q.join()def producer1(q): for i in range(5): q.put(i) print('生产者生产了%s' % i) q.join()if __name__ == '__main__': p1 = Process(target=producer0, args=(q,)) p2 = Process(target=producer1, args=(q,)) p3 = Process(target=consumer, args=(q,)) p3.daemon = True p1.start() p2.start() p3.start() p1.join() p2.join()
线程实现生产者消费者模型
from threading import Threadimport queueimport timeq = queue.Queue()def consumer(): while 1: res = q.get() time.sleep(2) print('消费者消费了%s' % res) q.task_done()def producer0(): for i in range(5): q.put(i) print('生产者0000生产了%s' % i) q.join()def producer1(): for i in range(5): q.put(i) print('生产者1111生产了%s' % i) q.join()if __name__ == '__main__': t1 = Thread(target=consumer,) t2 = Thread(target=producer0,) t3 = Thread(target=producer1,) t1.daemon = True t1.start() t2.start() t3.start() t2.join() t3.join()
回调函数
任务必须要有返回值以供回调函数调用,在add_done_callback中处理数据要先在任务中调用ret.result()将数据获取到~
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutorimport timedef task(n): time.sleep(2) return n ** 2def deal_result(ret): # print(ret) ret = ret.result() print(ret)if __name__ == '__main__': # p = ProcessPoolExecutor() p = ThreadPoolExecutor() # 默认20线程 p_list = [] # obj = p.map(task, range(10)) # print(list(obj)) for i in range(10): obj = p.submit(task, i).add_done_callback(deal_result) p_list.append(obj) p.shutdown() print([i.result() for i in p_list])
Queue
from multiprocessing import Process, JoinableQueueimport timeimport multiprocessingq = JoinableQueue()def consumer(q): while 1: res = q.get() time.sleep(2) print('消费者消费了%s' % res) q.task_done()def producer0(q): for i in range(5): q.put(i) print('生产者生产了%s' % i) q.join()def producer1(q): for i in range(5): q.put(i) print('生产者生产了%s' % i) q.join()if __name__ == '__main__': p1 = Process(target=producer0, args=(q,)) p2 = Process(target=producer1, args=(q,)) p3 = Process(target=consumer, args=(q,)) p3.daemon = True p1.start() p2.start() p3.start() p1.join() p2.join()
进程池线程池不能用Queue,如果想用,请参考下面的博客:
Python3之无法在进程池中使用队列Queue的解决方案
阅读全文
1 0
- Python3之网络编程总结
- Python3之socket网络编程
- python3网络编程
- Python3.XX---网络编程
- Python3网络编程
- Python3 网络编程
- Python3 网络编程
- python3.0网络编程
- Python3 网络编程
- Python3 网络编程
- Python3之基于UDP协议的网络编程
- Android网络编程之总结
- linux 网络编程总结(之网络编程函数总结)
- 网络编程--之socket编程知识点总结
- python3网络编程乱码问题解决
- Python3-网络编程Socket学习
- python3.x编程模板总结
- python3.6 编程技巧总结
- SPI总线之CPOL CPHA(转)
- Linux平台总线驱动设备模型
- ”vmware虚拟机迁移后无网卡eth0只有lo“的解决方法
- 在vos2009或3000上用户设置了费率不计费的解决方案
- upstart进程
- Python3之网络编程总结
- Xml文件之解析
- Qt学习之路(30): Graphics View Framework
- 贝塞尔曲线
- Spring Boot 1中使用MyBatis 3总结
- Qt的GraphicsView框架
- 关于科研学习的思考摸索
- redhat 6.5企业版下安装英特尔光纤万兆网卡
- 僵尸进程产生