#!/usr/bin/env python
# -*- coding: utf-8 -*- __author__ = "Daniel Altiparmak (sixfinger78@gmail.com)"
__copyright__ = "Copyright (C) 2015 Daniel Altiparmak"
__license__ = "GPL 3.0" import asyncio
import aiohttp
import tqdm import string
import random # get content and write it to file
def write_to_file(filename, content):
f = open(filename, 'wb')
f.write(content)
f.close() # a helper coroutine to perform GET requests:
@asyncio.coroutine
def get(*args, **kwargs):
response = yield from aiohttp.request('GET', *args, **kwargs)
return (yield from response.read_and_close()) @asyncio.coroutine
def download_file(url):
# this routine is protected by a semaphore
with (yield from r_semaphore):
content = yield from asyncio.async(get(url)) # create random filename
length = 10
file_string = ''.join(random.choice(
string.ascii_lowercase + string.digits) for _ in range(length)
)
filename = '{}.png'.format(file_string) write_to_file(filename, content) '''
make nice progressbar
install it by using `pip install tqdm`
'''
@asyncio.coroutine
def wait_with_progressbar(coros):
for f in tqdm.tqdm(asyncio.as_completed(coros), total=len(coros)):
yield from f images = ['http://lorempixel.com/1920/1920/' for i in range(100)] # avoid to many requests(coroutines) the same time.
# limit them by setting semaphores (simultaneous requests)
r_semaphore = asyncio.Semaphore(10) coroutines = [download_file(url) for url in images]
eloop = asyncio.get_event_loop()
#eloop.run_until_complete(asyncio.wait(coroutines))
eloop.run_until_complete(wait_with_progressbar(coroutines))
eloop.close()

  

aiohttp/asyncio 多次请求的更多相关文章

  1. aiohttp/asyncio测试代理是否可用

    #!/usr/bin/env python # encoding: utf-8 from proxyPool.db import RedisClient import asyncio import a ...

  2. aiohttp你不知道的异步操作网络请求

    aiohttp支持异步操作的网络请求的模块 1.一个简单异步协程爬取 read() text(encoding=编码) 比如:await r.text(encoding="utf-8&quo ...

  3. aiohttp 支持异步的网络请求模块

    通常在进行网络数据采集时候我们会用到requests,urllib等模块,但是这些模块在使用中并不支持异步,所以今天我们介绍一个支持异步网络请求的模块aiohttp. 首先我们使用flask简单的搭一 ...

  4. aiohttp/asyncio 小例子和解释

    #!/usr/bin/env python # encoding: utf-8 import aiohttp import asyncio import time # 通过async def定义的函数 ...

  5. Python学习---IO的异步[asyncio +aiohttp模块]

    aiohttp aiohttp是在asyncio模块基础上封装的一个支持HTTP请求的模块,内容比8.4.2[基于asyncio实现利用TCP模拟HTTP请求]更全面 安装aiohttp: pip3 ...

  6. 异步:asyncio和aiohttp的一些应用(1)

    1. asyncio 1.1asyncio/await 用法 async/await 是 python3.5中新加入的特性, 将异步从原来的yield 写法中解放出来,变得更加直观. 在3.5之前,如 ...

  7. 【译】深入理解python3.4中Asyncio库与Node.js的异步IO机制

    转载自http://xidui.github.io/2015/10/29/%E6%B7%B1%E5%85%A5%E7%90%86%E8%A7%A3python3-4-Asyncio%E5%BA%93% ...

  8. 用Python做大批量请求发送

    大批量请求发送需要考虑的几个因素: 1. 服务器承载能力(网络带宽/硬件配置); 2. 客户端IO情况, 客户端带宽, 硬件配置; 方案: 1. 方案都是相对的; 2. 因为这里我的情况是客户机只有一 ...

  9. Python黑魔法 --- 异步IO( asyncio) 协程

    python asyncio 网络模型有很多中,为了实现高并发也有很多方案,多线程,多进程.无论多线程和多进程,IO的调度更多取决于系统,而协程的方式,调度来自用户,用户可以在函数中yield一个状态 ...

随机推荐

  1. CentOS下使用Mysql

    安装过程百度,然后cd /etc->vi my.cnf修改配置文件,在mysqld下添加lower_case_table_name=1和character_set_server=utf8,保存退 ...

  2. wlr设置 Blog Ping

    ref:http://www.cnblogs.com/zhangyang/archive/2011/07/22/2113856.html 设置 Blog Ping 1.什么是Ping服务(Ping S ...

  3. Oozie 之 sqoop 实战

    1.创建 lib 目录并拷贝 mysql 支持包 2.修改 job.properties 文件 nameNode=hdfs://cen-ubuntu.cenzhongman.com:8020 jobT ...

  4. 1,MongoDB简介和安装

    一.初识MongoDB MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系数据库和非关系数 ...

  5. 《Cracking the Coding Interview》——第10章:可扩展性和存储空间限制——题目7

    2014-04-24 22:06 题目:搜索引擎问题,如果有一列100台服务器的集群,用来响应查询请求,你要如何设计query分发和cache策略? 解法:query分发可以用计算数字签名并对机器数取 ...

  6. 《Cracking the Coding Interview》——第6章:智力题——题目3

    2014-03-20 00:48 题目:有3升的瓶子和5升的瓶子,只允许倒满.倒到满为止.或是泼光三种操作,怎么搞出4升水呢? 解法:如果A和B是互质的两个正整数,且A<B,令X=B-A,则(X ...

  7. ClassNotFoundException: http.nio.NHttpClientEventHandle

    解决方案是打开maven仓库中的jar包看看报错的类所对应版本的类存在不存在,若不存在就换个版本的jar包

  8. mac安装虚拟机VirtualBox,并在虚拟机上安装centos

    1. 首先从网页上https://www.virtualbox.org/wiki/Downloads下载VirtualBox-6.0.0-127566-OSXdmg文件.我一般把下载的文件放到/opt ...

  9. (笔记) RealTimeRender[实时渲染] C3

    @author: 白袍小道 转载表明,查看随缘 前言: 从历史上看,图形加速始于每个像素扫描线上的插值颜色重叠一个三角形,然后显示这些值.包括访问图像数据允许纹理应用于表面.添加硬件 插入和测试z深度 ...

  10. XML转译字符

    &(逻辑与) & <(小于) < >(大于) > "(双引号) " '(单引号) &apos; [/size]