zoukankan      html  css  js  c++  java
  • [Gevent]gevent 网络抓取问答

    我听说过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/)原创文章,转载文章,请声明。

  • 相关阅读:
    陶瓷电容的结构、工艺、失效模式
    Vue.js最佳实践
    Vue 超快速学习
    CSS 小技巧
    HTML5 Canvas
    webkit下面的CSS设置滚动条
    Some untracked working tree files would be overwritten by checkout. Please move or remove them before you can checkout. View them
    JSCS: Please specify path to 'JSCS' package
    React中ref的使用方法
    React 60S倒计时
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4615464.html
Copyright © 2011-2022 走看看