python 之 线程池实现并发
使用线程池实现高IO并发
模块:ThreadPoolExecutor, as_completed
测试代码如下:
#!/opt/python3/bin/python3 from concurrent.futures import ThreadPoolExecutor, as_completed
import time def test(arg1, arg2, arg3):
time.sleep(int(arg1))
print('参数1:%s 参数2:%s 参数3:%s' % (arg1,arg2,arg3))
return arg1 # 创建含3个线程的线程池
with ThreadPoolExecutor(3) as executor:
# 生成所有任务
all_task = [executor.submit(test, ag1, ag2, ag3) for ag1, ag2, ag3 in [('2','aa1','aa2'),('3','bb1','bb2')]] # 等待任务全部执行完毕后,使用for的result方法循环返回结果
for out in as_completed(result):
mess = out.result()
print(mess) # as_completed 方法是等待result任务全部执行完毕
# result 方法是提取任务返回的结果
python 之 线程池实现并发的更多相关文章
- 设计模式:基于线程池的并发Visitor模式
1.前言 第二篇设计模式的文章我们谈谈Visitor模式. 当然,不是简单的列个的demo,我们以电商网站中的购物车功能为背景,使用线程池实现并发的Visitor模式,并聊聊其中的几个关键点. 一,基 ...
- Python的线程池实现
# -*- coding: utf-8 -*- #Python的线程池实现 import Queue import threading import sys import time import ur ...
- python 并发编程 基于线程池实现并发的套接字通信
不应该让服务端随着 并发的客户端数量增多,而无数起线程,应该用线程池,限制线程数量,控制最大并发数 io密集型程序,最大并发数是2 客户端 from socket import * client = ...
- Python之路【第八篇】python实现线程池
线程池概念 什么是线程池?诸如web服务器.数据库服务器.文件服务器和邮件服务器等许多服务器应用都面向处理来自某些远程来源的大量短小的任务.构建服务器应用程序的一个过于简单的模型是:每当一个请求到达就 ...
- 《Python》线程池、携程
一.线程池(concurrent.futures模块) #1 介绍 concurrent.futures模块提供了高度封装的异步调用接口 ThreadPoolExecutor:线程池,提供异步调用 P ...
- [python] ThreadPoolExecutor线程池
初识 Python中已经有了threading模块,为什么还需要线程池呢,线程池又是什么东西呢?在介绍线程同步的信号量机制的时候,举得例子是爬虫的例子,需要控制同时爬取的线程数,例子中创建了20个线程 ...
- python之线程和进程(并发编程)
python的GIL In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native ...
- Python之线程池
版本一: #!/usr/bin/env python # -*- coding:utf-8 -*- import Queue import threading class ThreadPool(obj ...
- 线程池——JAVA并发编程指南
TPS00-J. 用线程池实现应用在流量暴涨时优雅降级 很多程序都要解决这样一个问题——处理一系列外来的请求.Thread- Per-Message这种设计模式是最简单的并发策略了,它为每一个请求创建 ...
随机推荐
- python入门:CONTINUE 的作用 跳出本次循环后,重新开始循环
#!/usr/bin/env python # -*- coding:utf-8 -*- # CONTINUE 的作用 跳出本次循环后,重新开始循环 import time while True: ' ...
- Thonny -- 简洁的 python 轻量级 IDE
Thonny目前是 树莓派 上 默认的 Python 开发环境. 该 IDE 是 Institute of Computer Science of University of Tartu (爱沙尼亚 ...
- LeetCode(155) Min Stack
题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...
- eclipse使用技巧的网站收集——转载(一)
Eclipse工具使用技巧总结(转载) 首先推荐一篇非常好的How to use eclipse文章 ,讲的是eclipse使用的方方面面,非常实用,推荐给大家! 一.常用快捷键:Ctrl+F11 运 ...
- selenium2用AutoIt上传文件
1.标签是input,如下图所示: WebElement e1= driver.findElement(By.id("load"));//输入要上传文件的地址e1.sendKeys ...
- 《Scrum实战》第1课【知易行难】全团课后任务汇总
1组 孟帅(班长) kecyru 2017-7-5 http://kecyru.blog.163.com/blog/static/27416617320176411513013 htt ...
- webdriver高级应用- 使用Chrome浏览器自动将文件下载到指定路径
#encoding=utf-8 from selenium import webdriver import unittest, time class TestDemo(unittest.TestCas ...
- Install Oracle 11G Release 2 (11.2) on Centos Linux 7
Install Oracle 11G Release 2 (11.2) on Centos Linux 7 This article presents how to install Oracle 11 ...
- Clarke and five-pointed star
Clarke is a patient with multiple personality disorder. One day, Clarke turned into a learner of geo ...
- oracle dual表用途及结构详解
dual是一个虚拟表,用来构成select的语法规则,oracle保证dual里面永远只有一条记录.我们可以用它来做很多事情,如下: 1.查看当前用户,可以在 SQL Plus中执行下面语句 sele ...