Skip to content

Instantly share code, notes, and snippets.

@wwwins
Created March 31, 2017 04:39
Show Gist options
  • Save wwwins/90ecbb0af1879d29319a1d1caa2a476c to your computer and use it in GitHub Desktop.
Save wwwins/90ecbb0af1879d29319a1d1caa2a476c to your computer and use it in GitHub Desktop.
gevent example
# -*- coding: utf-8 -*-
import gevent
from gevent.queue import Queue
from urllib2 import urlopen
from gevent.threadpool import ThreadPool
def worker(n):
while not tasks.empty():
task = tasks.get()
print('Worker %s got task %s' % (n, task))
gevent.sleep(0)
print('Quitting time!')
def boss():
print('Boss')
for i in xrange(1,25):
tasks.put_nowait(i)
cnt = 0
while cnt < 30:
if cnt == 5:
add_client()
if cnt == 10:
ceo()
if result["code"] > 0:
print("http finish")
#pool.kill()
gevent.sleep(0.1)
print("result:{}, pool:{}".format(result["code"],len(pool)))
cnt = cnt + 1
return "boss 881"
def ceo():
# joinall 同時加入
# worker1--->
# worker2--->
# worker3--->
gevent.joinall([
gevent.spawn(worker, 'steve'),
gevent.spawn(worker, 'john'),
gevent.spawn(worker, 'nancy'),
])
# 分別 join 1做完再做2再做3依序下去
# worker1--->
# worker2--->
# worker3--->
# gevent.spawn(worker, 'steve').join()
# gevent.spawn(worker, 'john').join()
# gevent.spawn(worker, 'nancy').join()
def add_client():
pool.spawn(get_header,"http://www.yandex.ru")
def get_header(url):
print('Starting %s' % url)
data = urlopen(url).read()
result["code"] = len(data)
print('%s: %s bytes: %r' % (url, len(data), data[:50]))
tasks = Queue()
# global object
result = {}
result["code"] = 0
pool = ThreadPool(10)
# gevent.spawn(boss).join() # blocking
# same as
r = pool.spawn(boss)
# waiting for boss threading have finished
#gevent.wait([r])
# waiting for all greenlets have finished
gevent.wait()
print("thread return value:{}".format(r.value))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment