gevent

對於gevent本質概念不是很瞭解,雖然看過很多文章,似懂非懂!

# coding=utf-8


import gevent
import urllib2
import time

def fetch(pid, url):
    print('Process %s: %s start work' % (pid, url))
    flag = None
    status = None
    error_msg = None
    try:
        req_obj = urllib2.Request(url)
        response = urllib2.urlopen(req_obj, timeout=10)
        status = response.code
        result = response.read()
        flag = True
    except Exception, e :
        error_msg = str(e)
        flag = False

    print('Process %s: %s %s' % (pid, url, status))
    return (pid, flag, url, status, error_msg)

def asynchronous():
    i = 0
    jobs = []
    url = 'https://github.com/'
    for i in range(0, 10) :
        jobs.append(gevent.spawn(fetch, i, url))
    gevent.joinall(jobs)
    value = []
    #for job in jobs:
 #      print job.value
        #value.append(job.value)

start = time.time()
print('Asynchronous:')
asynchronous()
stop = time.time()
print stop-start

運行結果爲`Asynchronous:
Process 0: https://github.com/ start work
Process 0: https://github.com/ 200
Process 1: https://github.com/ start work
Process 1: https://github.com/ 200
Process 2: https://github.com/ start work
Process 2: https://github.com/ 200
Process 3: https://github.com/ start work
Process 3: https://github.com/ 200
Process 4: https://github.com/ start work
Process 4: https://github.com/ 200
Process 5: https://github.com/ start work
Process 5: https://github.com/ 200
Process 6: https://github.com/ start work
Process 6: https://github.com/ 200
Process 7: https://github.com/ start work
Process 7: https://github.com/ 200
Process 8: https://github.com/ start work
Process 8: https://github.com/ 200
Process 9: https://github.com/ start work
Process 9: https://github.com/ 200
24.4027779102

Process finished with exit code 0`

說明這個請求是串行的!並沒有在阻塞的時候起到作用!

如果添加一行 gevent.sleep(0)看看有啥反應

# coding=utf-8

import gevent
import urllib2
import time

def fetch(pid, url):
    print('Process %s: %s start work' % (pid, url))
    flag = None
    status = None
    error_msg = None
    try :
        req_obj = urllib2.Request(url)
        gevent.sleep(0)
        response = urllib2.urlopen(req_obj, timeout=10)
        status = response.code
        result = response.read()
        flag = True
    except Exception, e :
        print e
        error_msg = str(e)
        flag = False

    print('Process %s: %s %s' % (pid, url, status))
    return (pid, flag, url, status, error_msg)

def asynchronous():
    i = 0
    jobs = []
    url = 'https://github.com/'
    for i in range(0, 3) :
        jobs.append(gevent.spawn(fetch, i, url))
    gevent.joinall(jobs)
    value = []
    for job in jobs:
 #      print job.value
        value.append(job.value)

start = time.time()
print('Asynchronous:')
asynchronous()
stop = time.time()
print stop-start

結果:

Asynchronous:
Process 0: https://github.com/ start work
Process 1: https://github.com/ start work
Process 2: https://github.com/ start work
Process 3: https://github.com/ start work
Process 4: https://github.com/ start work
Process 5: https://github.com/ start work
Process 6: https://github.com/ start work
Process 7: https://github.com/ start work
Process 8: https://github.com/ start work
Process 9: https://github.com/ start work
Process 0: https://github.com/ 200
Process 1: https://github.com/ 200
Process 2: https://github.com/ 200
Process 3: https://github.com/ 200
Process 4: https://github.com/ 200
Process 5: https://github.com/ 200
Process 6: https://github.com/ 200
Process 7: https://github.com/ 200
Process 8: https://github.com/ 200
Process 9: https://github.com/ 200
24.3354411125

Process finished with exit code 0

還是要這麼久!
第三次改一下
import gevent.monkey
gevent.monkey.patch_all()
刪掉第二次添加的gevent.sleep(0)

# coding=utf-8

import gevent.monkey
gevent.monkey.patch_all()


import gevent
import urllib2
import time

def fetch(pid, url):
    print('Process %s: %s start work' % (pid, url))
    flag = None
    status = None
    error_msg = None
    try :
        req_obj = urllib2.Request(url)
        response = urllib2.urlopen(req_obj, timeout=10)
        status = response.code
        result = response.read()
        flag = True
    except Exception, e:
        print e
        error_msg = str(e)
        flag = False

    print('Process %s: %s %s' % (pid, url, status))
    return (pid, flag, url, status, error_msg)

def asynchronous():
    i = 0
    jobs = []
    url = 'https://github.com/'
    for i in range(0, 10):
        jobs.append(gevent.spawn(fetch, i, url))
    gevent.joinall(jobs)
    value = []
    #for job in jobs:
 #      print job.value
        #value.append(job.value)

start = time.time()
print('Asynchronous:')
asynchronous()
stop = time.time()
print stop-start

結果如下:

Asynchronous:
Process 0: https://github.com/ start work
Process 1: https://github.com/ start work
Process 2: https://github.com/ start work
Process 3: https://github.com/ start work
Process 4: https://github.com/ start work
Process 5: https://github.com/ start work
Process 6: https://github.com/ start work
Process 7: https://github.com/ start work
Process 8: https://github.com/ start work
Process 9: https://github.com/ start work
Process 2: https://github.com/ 200
Process 8: https://github.com/ 200
Process 9: https://github.com/ 200
Process 3: https://github.com/ 200
Process 0: https://github.com/ 200
Process 4: https://github.com/ 200
Process 6: https://github.com/ 200
Process 7: https://github.com/ 200
Process 5: https://github.com/ 200
Process 1: https://github.com/ 200
3.27638602257

Process finished with exit code 0

瞬間快的不要不要的。但是不知道原因,尷尬!慢慢研究中!
ps:上述代碼來自http://www.firefoxbug.com/index.php/archives/2750/
原作者第三次代碼有問題,應該爲gevent.monkey.patch_all()而不是gevent.monkey.patch_socket()。話說http請求算是socket?
望大家注意!這篇文章還不能評論,尷尬!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章