我听说过gevent基于事件的异步处理功能 如何高效率,该项目已很少使用,今天是没什么学习一些简单的使用。

有正式书面一个非常好的教程 中国版的地址:http://xlambda.com/gevent-tutorial/ 学习gevent非常不错的资料。

详细的理论这里不怎么说了,仅仅是有些了解。详细的原理还不能解释的非常清楚。

只是协程这样的概念在golang里面非常多。

写了一个訪问网络,使用同步操作,gevent 和 多线程对照的样例。

#!/usr/bin/python
# -*- coding: utf-8 -*-
# python2.7x
# gevent_urllib2.py
# author: orangelliu
# date: 2014-08-20 import gevent.monkey
gevent.monkey.patch_socket() import gevent
import urllib2
import json
import threading def fetch(pid):
response = urllib2.urlopen('http://www.orangleliu.info')
result = response.read()
btypes = len(result) print 'process %s : %s'%(pid, btypes) def synchronous():
for i in range(10):
fetch(i) def asynchonous():
threads = []
for i in range(10):
threads.append(gevent.spawn(fetch,i))
gevent.joinall(threads) def mulithread():
threads = []
for i in range(10):
th = threading.Thread(target=fetch, args=(i,))
threads.append(th) for thread in threads:
thread.start() for thread in threads:
threading.Thread.join(thread) import time
print 'sync....'
ss = time.time()
synchronous()
print 'sync time is %s'%(time.time()-ss) print 'async'
sa = time.time()
asynchonous()
print 'async time is %s'%(time.time()-sa) print 'async'
sm = time.time()
mulithread()
print 'thread time is %s'%(time.time()-sm)

这结果仅仅能作为參考。由于不同的时间网络状况有差异,可是总的来说多线程最快。gevent还行,同步最慢。

可是考虑到gevent的开销非常小。所以还是非常具有性价比的。

还有从结果中能够看到gevent和多线程都会有上下文切换,所以运行结果的线程id是乱序的,这个非常好理解。

sync....
process 0 : 8657
process 1 : 8657
process 2 : 8657
process 3 : 8657
process 4 : 8657
process 5 : 8657
process 6 : 8657
process 7 : 8657
process 8 : 8657
process 9 : 8657
sync time is 2.7610001564
async
process 8 : 8657
process 7 : 8657
process 6 : 8657
process 2 : 8657
process 5 : 8657
process 3 : 8657
process 0 : 8657
process 4 : 8657
process 1 : 8657
process 9 : 8657
async time is 1.50199985504
async
process 0 : 8657
process 1 : 8657
process 3 : 8657
process 4 : 8657
process 5 : 8657
process 7 : 8657
process 9 : 8657
process 8 : 8657
process 6 : 8657
process 2 : 8657
thread time is 0.986000061035

本文出自 “orangleliu笔记本”博客,请务必保留此出处http://blog.csdn.net/orangleliu/article/details/38715763

版权声明:本文orangleliu(http://blog.csdn.net/orangleliu/)原创文章,转载文章,请声明。

[Gevent]gevent 网络抓取问答的更多相关文章

  1. 网络抓取功能实现 将获取的结果进行过滤并写入到TXT文档中

    下面是自己编写的 网络抓取功能实现 将获取的结果进行过滤并写入到TXT文档中 (以防忘记) 原创哟 import java.io.BufferedReader;import java.io.Buffe ...

  2. 网络爬虫:使用Scrapy框架编写一个抓取书籍信息的爬虫服务

      上周学习了BeautifulSoup的基础知识并用它完成了一个网络爬虫( 使用Beautiful Soup编写一个爬虫 系列随笔汇总 ), BeautifulSoup是一个非常流行的Python网 ...

  3. iOS开发——网络使用技术OC篇&网络爬虫-使用正则表达式抓取网络数据

    网络爬虫-使用正则表达式抓取网络数据 关于网络数据抓取不仅仅在iOS开发中有,其他开发中也有,也叫网络爬虫,大致分为两种方式实现 1:正则表达 2:利用其他语言的工具包:java/Python 先来看 ...

  4. 抓取“矢量”的实时交通流量数据

    1. 引言 最近老师有一个需求,就是想要抓取实时的矢量交通流量数据来做分析,类似于百度地图,高德地图的"实时路况"那种.平时的网络抓取工作一般是抓取网页上现成的数据,但是交通流量数 ...

  5. 【爬虫】利用Scrapy抓取京东商品、豆瓣电影、技术问题

    1.scrapy基本了解 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架.可以应用在包括数据挖掘, 信息处理或存储历史数据等一系列的程序中.其最初是为了页面抓取(更确切来说,网络抓 ...

  6. 网页抓取工具Teleport Ultra简介及如何使用

    Teleport Ultra是一款专业的离线浏览器,能够快速.准确地从网络抓取数据并保存到本地,实现离线浏览的目的.它可以从Internet的任何地方抓回你想要的任何文件,它可以在你指定的时间自动登录 ...

  7. 使用Node.js实现简单的网络爬取

    由于最近要实现一个爬取H5游戏的代理服务器,隧看到这么一篇不错的文章(http://blog.miguelgrinberg.com/post/easy-web-scraping-with-nodejs ...

  8. Python抓取国家医疗费用数据:国家名、人均开销

    前言 整个世界正被大流行困扰着,不同国家拿出了不同的应对策略,也取得了不同效果.这也是本文的脑洞来源,打算研究一下各国在医疗基础设置上的开支,对几个国家的医疗费用进行数据可视化. 由于没有找到最近一年 ...

  9. 网络爬虫: 从allitebooks.com抓取书籍信息并从amazon.com抓取价格(3): 抓取amazon.com价格

    通过上一篇随笔的处理,我们已经拿到了书的书名和ISBN码.(网络爬虫: 从allitebooks.com抓取书籍信息并从amazon.com抓取价格(2): 抓取allitebooks.com书籍信息 ...

随机推荐

  1. thinkphp3.2使用join联合查询

    $members=$model->table('zhope_card A') ->join('zhope_user U ON A.adduser=U.id',"LEFT" ...

  2. 【24.17%】【codeforces 721D】Maxim and Array

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 创建ListView的基本步骤 分类: H1_ANDROID 2013-10-31 23:25 1276人阅读 评论(0) 收藏

    参考<疯狂android讲义>第2.5节P94 1.创建一个或者多个ListView <LinearLayout xmlns:android="http://schemas ...

  4. mysql的四种启动方式

    查看该版本的相应参数: mysqld --verbose --help     1.mysqld  ./mysqld --defaults-file=/etc/my.cnf --user=mysql ...

  5. 【t030】数字构造

    Time Limit: 3 second Memory Limit: 256 MB [问题描述] 有这么一个游戏: 写出一个1-N的排列a[i],然后每次将相邻两个数相加,构成新的序列,再对新序列进行 ...

  6. [React] Keep Application State in Sync with Browser History

    Using pushState and passing route data via context allows our application to respond to route change ...

  7. C++ 快速入门笔记:基本语法

    数据类型 枚举类型 enum color { red, green, blue } colors; colors = blue; 默认情况下,第一个名称的值是 0,后面的依次加 1.也可以自定初始值: ...

  8. Hive的日期函数

    1.unix时间戳转时间函数 语法: from_unixtime(bigint unixtime[, string format]) 返回值: string 说明: 转化UNIX时间戳(从1970-0 ...

  9. Python 库的使用 —— dis

    dis:Disassembler of Python byte code into mnemonics. Java.Python.Ruby 1.9 这些语言均使用了栈机器型的 VM.因为是基于栈的实现 ...

  10. MapKit

    MapKit MapKit框架的使用 nMapKit框架使用前提 p导入框架 p p导入主头文件 #import <MapKit/MapKit.h> MapKit框架使用须知 pMapKi ...