1 import gevent
2 import time
3
4 def func1():
5 print("func1: start.....")
6 # Put the current greenlet to sleep for at least *seconds*.(模拟I/O操作,任务在此处自动切换)
7 gevent.sleep(3)
8 print("func1: end")
9
10 def func2():
11 print("func2: start.....")
12 gevent.sleep(0.5)
13 print("func2: end")
14
15 start_time = time.time()
16 # joinall(greenlets, timeout=None, raise_error=False, count=None)
17 # Wait for the ``greenlets`` to finish.
18 # :return: A sequence of the greenlets that finished before the timeout (if any)expired.
19 gevent.joinall([gevent.spawn(func1),
20 gevent.spawn(func2)])
21 # spawn(cls, *args, **kwargs)
22 # Create a new :class:`Greenlet` object and schedule it to run ``function(*args, **kwargs)``.
23 # This can be used as ``gevent.spawn`` or ``Greenlet.spawn``.
24
25 print("cost:", time.time()-start_time)
26 # 通过计算程序运行的时间可以发现程序确实是以单线程达模拟出了多任务并行的操作。