asyncio Queue的使用例子
import aiohttp
import asyncio
import async_timeout
from urllib.parse import urljoin, urldefrag root_url = "http://python.org/"
crawled_urls, url_hub = [], [root_url, "%s/sitemap.xml" % (root_url), "%s/robots.txt" % (root_url)]
headers = {'user-agent': 'Opera/9.80 (X11; Linux x86_64; U; en) Presto/2.2.15 Version/10.10'} async def get_body(url):
async with aiohttp.ClientSession() as session:
try:
with async_timeout.timeout(10):
async with session.get(url, headers=headers) as response:
if response.status == 200:
html = await response.text()
return {'error': '', 'html': html}
else:
return {'error': response.status, 'html': ''}
except Exception as err:
return {'error': err, 'html': ''} async def handle_task(task_id, work_queue):
while not work_queue.empty():
queue_url = await work_queue.get()
if not queue_url in crawled_urls:
crawled_urls.append(queue_url)
body = await get_body(queue_url)
if not body['error']:
for new_url in get_urls(body['html']):
if root_url in new_url and not new_url in crawled_urls:
work_queue.put_nowait(new_url)
else:
print(f"Error: {body['error']} - {queue_url}") def remove_fragment(url):
pure_url, frag = urldefrag(url)
return pure_url def get_urls(html):
new_urls = [url.split('"')[0] for url in str(html).replace("'",'"').split('href="')[1:]]
return [urljoin(root_url, remove_fragment(new_url)) for new_url in new_urls] if __name__ == "__main__":
q = asyncio.Queue()
[q.put_nowait(url) for url in url_hub]
loop = asyncio.get_event_loop()
tasks = [handle_task(task_id, q) for task_id in range(3)]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
for u in crawled_urls:
print(u)
print('-'*30)
print(len(crawled_urls))
asyncio Queue的使用例子的更多相关文章
- python中利用队列asyncio.Queue进行通讯详解
python中利用队列asyncio.Queue进行通讯详解 本文主要给大家介绍了关于python用队列asyncio.Queue通讯的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细 ...
- threading包的例子和queue包的例子
参考:https://www.cnblogs.com/tkqasn/p/5700281.html 参考:https://www.cnblogs.com/tkqasn/p/5700281.html th ...
- asyncio queue
from asyncio import Queue,sleep import asyncio from threading import Thread import time qu=Queue() # ...
- rabbit的简单搭建,java使用rabbitmq queue的简单例子和一些坑
一 整合 由于本人的码云太多太乱了,于是决定一个一个的整合到一个springboot项目里面. 附上自己的项目地址https://github.com/247292980/spring-boot 以整 ...
- python 多进程使用Queue通信的例子
import time from multiprocessing import Process,Queue MSG_QUEUE = Queue(5) def startA(msgQueue): whi ...
- python异步编程模块asyncio学习(二)
尽管asyncio应用通常作为单线程运行,不过仍被构建为并发应用.由于I/O以及其他外部事件的延迟和中断,每个协程或任务可能按一种不可预知的顺序执行.为了支持安全的并发执行,asyncio包含了thr ...
- 深入Asyncio(二)从线程到协程
线程的真相 多线程并不是一无是处,在实际问题中,要权衡优劣势来选择多线程.多进程或是协程.协程为多线程的某些问题提供了一种解决方案,所以学习协程首先要对线程有一定了解. 多线程优点 代码可读性 多线程 ...
- 深入Asyncio(三)Asyncio初体验
Asyncio初体验 Asyncio在Python中提供的API很复杂,其旨在替不同群体的人解决不同的问题,也正是由于这个原因,所以很难区分重点. 可以根据asyncio在Python中的特性,将其划 ...
- asyncio:python3未来并发编程主流、充满野心的模块
介绍 asyncio是Python在3.5中正式引入的标准库,这是Python未来的并发编程的主流,非常重要的一个模块.有一个web框架叫sanic,就是基于asyncio,语法和flask类似,使用 ...
随机推荐
- Event Recommendation Engine Challenge分步解析第五步
一.请知晓 本文是基于: Event Recommendation Engine Challenge分步解析第一步 Event Recommendation Engine Challenge分步解析第 ...
- 【MSSQL】SqlServer中delete语句表别名的问题
1.一般情况下删除表数据的sql语句: delete from products 2.如果想给表起个别名再删除呢,就得像下面这样写了 delete products from products as ...
- Java NIO系列教程(七) selector原理 Epoll版的Selector
目录: Reactor(反应堆)和Proactor(前摄器) <I/O模型之三:两种高性能 I/O 设计模式 Reactor 和 Proactor> <[转]第8章 前摄器(Proa ...
- JAVA核心技术I---JAVA基础知识(知识回顾)
一:多态问题 class Father { public void hello() { System.out.println("Father says hello."); } } ...
- MassTransit 学习
http://blog.csdn.net/starfd/article/details/50973124
- 关于js事件执行顺序
关于js事件执行顺序小技巧 js事件执行顺序是js中一个老生常谈的一个话题, 聊这个话题之前我们先谈谈怎么给页面元素绑定我们需要的事件 1.给页面元素绑定事件 a)直接在元素上面加上需要绑定的事件,如 ...
- 引用mchange-commons-java-0.2.3.4.jar架包
pom.xml中增加 <!-- https://mvnrepository.com/artifact/com.mchange/mchange-commons-java --> <de ...
- vue cli 解决跨域 线上 nginx 反向代理配置
前后分离 axios 接 api 跨域问题如图: 解决办法: 1. npm start 本地开发环境解决: 在webpack配置文件 /config/index.js 里找到 proxyTable 开 ...
- jquery判断点击鼠标左、中、右键事件
注:1为鼠标左键.2为鼠标中键.3为鼠标右键$('#btn').mousedown(function(e){ if(3 == e.which){ al ...
- 关于Java 实现抽象类的抽象方法的特性的利用---面向切面
今天看工作看代码突然有了以下设想: /** * Created by zq on 2017/5/25. * 抽象类 */ public abstract class AbstractC { publi ...