#!/usr/bin/env python
# encoding: utf-8
# Date: 2018/6/19
# # from gevent import monkey # 這倆行必須放在首行,下面就能夠使用time(非gevent)的io
# # monkey.patch_all() # 至關於把全部的io操做打了標記,把io阻塞操做變成了非阻塞操做
# # gevent遇到io均可以自動切換
# import gevent
# import time
#
#
# def eat(name):
# print('%s eat 1' % name)
# gevent.sleep(3) # 至關於io操做
# # time.sleep(3) # gevent不識別,只是別本身的io
# print('%s eat 2' % name)
#
#
# def play(name):
# print('%s play 1' % name)
# gevent.sleep(4)
# print('%s play 2' % name)
#
#
# start_time = time.time()
# g1 = gevent.spawn(eat, 'egon') # 提交任務是異步提交
# g2 = gevent.spawn(play, 'alex')
#
# g1.join()
# g2.join()
# stop_time = time.time()
# print(stop_time - start_time) # 4.005170583724976
# gevent 異步提交任務
from gevent import monkey;monkey.patch_all() # 必須在首行
import gevent
import time
def eat(name):
print('%s eat 1' % name)
time.sleep(3)
print('%s eat 2' % name)
def play(name):
print('%s play 1' % name)
time.sleep(4)
print('%s play 2' % name)
g1 = gevent.spawn(eat, 'egon') # 提交任務是異步提交
g2 = gevent.spawn(play, 'alex')
# time.sleep(5) # 保證線程在5秒內能夠執行完
# g1.join() # 同上一行代碼,不用考慮時間
# g2.join()
# 終極
gevent.joinall([g1, g2])
python