python(gevern 自动io切换)

# Author: Sooele

import gevent

def func1():
    print('\033[31;1mSooele...\033[0m')
    gevent.sleep(2)
    print('\033[31;1m CCCBBB...\033[0m')

def func2():
    print('\033[32;1mSooelebbb...\033[0m')
    gevent.sleep(1)
    print('\033[32;1mCOOOOOOBBBB...\033[0m')


gevent.joinall([
    gevent.spawn(func1),
    gevent.spawn(func2),
    #gevent.spawn(func3),
])
import gevent

def foo():
    print('Running in foo')
    gevent.sleep(2)
    print('Explicit context switch to foo again')
def bar():
    print('Explicit精确的 context内容 to bar')
    gevent.sleep(1)
    print('Implicit context switch back to bar')
def func3():
    print("running func3 ")
    gevent.sleep(0)
    print("running func3  again ")


gevent.joinall([
    gevent.spawn(foo), #生成,
    gevent.spawn(bar),
    gevent.spawn(func3),
])

 

发表评论