Today read the blog get a fun module called Trip # (PIP install trip)
Compatible with version 2.7
Based on two large dependency packages: TRIP: Tornado & Requests In Pair
.
Let's take a look at simple code:
Import Trip@trip.coroutine def Main (): yield trip.get ('http://www.baidu.com/') print(r.content) Trip.run (main)
And then a comparison was made:
ImportTime , FunctoolsImportRequests,tripdefTimeit (FN): Start_time=time.time () fn ()returnTime.time ()-Start_timeurl='https://www.baidu.com/' Times= 100defFetch (): R= [Requests.get (URL) forIinchrange (times)]returnR@trip.coroutinedefAsync_fetch (): R=yield[Trip.get (URL) forIinchrange (times)]RaiseTrip . Return (R)Print("[+]non-trip Cost:%ss"%Timeit (Fetch))Print("[+]trip Cost:%ss"% Timeit (functools.partial (Trip.run,async_fetch)))
#result
#[+]non-trip cost:14.9129998684s
#[+]trip cost:1.83399987221s
14.9 seconds and 1.8 seconds gap, the effect is obvious!
In the reptile comparison, the common crawler:
Import'http://httpbin.org'= requests. Session ()def fetch (times=10): s.get ('%s/cookies/set?name=value ' % url' = [S.get ('%s/get' for in range (times)] Print Rfetch ()
After adding trip optimization:
ImportTripurl='http://httpbin.org's=Trip . Session () @trip. CoroutinedefFetch (times=10): yieldS.get ('%s/cookies/set?name=value'%URL) r=yield[S.get ('%s/get'% URL) forIinchrange (times)]Printrtrip.run (Fetch)
The change is not big on the original basis.
Python base = = = based on the requests module on the coprocessor "trip"