Tags » Gevent

Python Gevent 1.1 finally released

This is the first release of gevent 1.1, featuring support for Python 3 and PyPy, as well as a host of other fixes and additions. 15 more words

Programming

How we use gevent to go fast

“Not too long ago we were tackling the challenge of fixing a legacy Python system and converting a two-year old single-threaded codebase with hundreds of thousands of lines of code to a multi-threaded codebase. 55 more words

Programming

关于gevent的问题

import gevent.monkey
gevent.monkey.patch_socket()
 
import gevent
import urllib2
import simplejson as json

from gevent import queue
from gevent import pool
 
def fetch(pid):
    response = urllib2.urlopen('http://json-time.appspot.com/time.json')
    result = response.read()
    json_result = json.loads(result)
    datetime = json_result['datetime']
 
    print 'Process ', pid, datetime
    return json_result['datetime']

# worker_sequence = []
def asynchronous(worker_sequence):
    for i in range(1,10):
        g = gevent.spawn(fetch, i)
        worker_sequence.append(g)
    gevent.joinall(worker_sequence)
    print len(worker_sequence)

# worker_sequence = gevent.queue.Queue()
# or, worker_sequence = gevent.queue.JoinableQueue()
# error: "this operation would block forever" after all works are done 
def asynchronous(worker_sequence):
    for i in range(1,10):
        g = gevent.spawn(fetch, i)
        worker_sequence.put(g)
    gevent.joinall(worker_sequence)

# worker_poll = gevent.pool.Pool()
def asynchronous(worker_pool):
    for i in range(1,10):
        g = gevent.spawn(fetch, i)
        worker_pool.add(g)
    print len(worker_pool)
    gevent.joinall(worker_pool)
    print len(worker_pool)

threads = []
#threads = queue.JoinableQueue()
#threads = queue.Queue()
#threads = pool.Pool(100)

print 'Asynchronous:'
asynchronous(threads)
… 35 more words