# 10.py
#code=utf-8
#
# python 的 gevent 协程库使用
# 首先安装greelet,方式:pip install greenlet。下载gevent包,地址:https://pypi.python.org/packages/12/dc/0b2e57823225de86f6e111a65d212c9e3b64847dddaa19691a6cb94b0b2e/gevent-1.1.1.tar.gz#md5=1532f5396ab4d07a231f1935483be7c3,tar -zxvf 解压之后 执行python setup.py install
import gevent
from gevent.queue import Queue
def func1():
print 'start func1'
gevent.sleep(1)
print 'end func1'
def func2():
print 'start func2'
gevent.sleep(1)
print 'end func2'
gevent.joinall(
[
gevent.spawn(func1),
gevent.spawn(func2)
]
)
# 下面测试队列的使用
def func3():
for i in range(10):
print 'int the func3'
q.put('test')
def func4():
for i in range(10):
print 'int the func4'
res = q.get()
print '---->', res
q = Queue()
gevent.joinall(
[
gevent.spawn(func3),
gevent.spawn(func4)
]
)
'''
此打印结果:说明这两个func都进行执行了,然后都执行了start,end是后面执行结果
start func1
start func2
end func1
end func2
int the func3
int the func3
int the func3
int the func3
int the func3
int the func3
int the func3
int the func3
int the func3
int the func3
int the func4
----> test
int the func4
----> test
int the func4
----> test
int the func4
----> test
int the func4
----> test
int the func4
----> test
int the func4
----> test
int the func4
----> test
int the func4
----> test
int the func4
----> test
'''