[Gevent]gevent Network Crawl Quiz

Source: Internet
Author: User

I've heard that the Gevent event-based asynchronous processing feature is highly efficient and the project has been rarely used, and today is nothing to learn some simple use of.

There is a very good tutorial in the official writing of the Chinese version of the address: http://xlambda.com/gevent-tutorial/Learning gevent very good information.


The detailed theory here is not how to say, just a little understanding. The detailed principle cannot be explained very clearly.

It's just that the concept of co-Golang is very much inside.

Wrote a visit to the network, using synchronous operation, gevent and multi-threaded control examples.


#!/usr/bin/python#-*-coding:utf-8-*-# python2.7x# gevent_urllib2.py# author:orangelliu# date:2014-08-20import geven T.monkeygevent.monkey.patch_socket () Import geventimport urllib2import jsonimport threadingdef 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 (+): Fetch (i) def asynchonous (): Threads = []for i in Range]: Threads.append ( Gevent.spawn (Fetch,i)) Gevent.joinall (Threads) def mulithread (): Threads = []for i in range]: th = Threading. The thread (Target=fetch, args= (i,)) threads.append (TH) for the thread in the Threads:thread.start () for the thread in threads:threading. Thread.Join (thread) import timeprint ' 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)

The result can only be taken as an examination. Because of the different time network condition has the difference, but generally multithreading fastest. Gevent is OK, synchronization is the slowest.

But considering the cost of gevent is very small . So it is very cost-effective.

There is a context switch from the results to see gevent and multithreading, so the thread ID of the running result is out of order, which is very well understood.

Sync....process 0:8657process 1:8657process 2:8657process 3:8657process 4:8657process 5:8657process 6:8657PR Ocess 7:8657process 8:8657process 9:8657sync time is 2.7610001564asyncprocess 8:8657process 7:8657process 6:86 57process 2:8657process 5:8657process 3:8657process 0:8657process 4:8657process 1:8657process 9:8657async ti Me is 1.50199985504asyncprocess 0:8657process 1:8657process 3:8657process 4:8657process 5:8657process 7:8657PR Ocess 9:8657process 8:8657process 6:8657process 2:8657thread time is 0.986000061035
This article is derived from"Orangleliu Notebook" Blog, be sure to keep this source http://blog.csdn.net/orangleliu/article/details/38715763




Copyright notice: This article Orangleliu (http://blog.csdn.net/orangleliu/) original article, reprint article, please declare.

[Gevent]gevent Network Crawl Quiz

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.