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