yield与gen.coroutine

yield与gen.coroutine
def d():
    for i in range(2):
        yield i

def b():
    yield d()
    print("b")
    yield "bb"

def a():
    g_b = b()
    print(g_b)
    g_d = next(g_b)
    print(g_d)
    v_d = next(g_d)
    print(v_d) # 0
    v_d = next(g_d)
    print(v_d) # 1
    b_next = next(g_b)
    print(b_next) # bb

if __name__ == "__main__":
    a()
#---输出
# 
# 
# 0
# 1
# b
# bb

----------------------
def d():
    for i in range(2):
        yield i

def b():
    yield d()
    print("b")

def a():
    g_b = b()
    print(g_b) # 
    g_d = next(g_b)
    print(g_d) # 
    v_d = next(g_d)
    print(v_d) # 0
    v_d = next(g_d)
    print(v_d) # 1
    b_next = next(g_b)
    print(b_next)
#---输出


0
1
b
Traceback (most recent call last):
  File "E:\eclipse-workspace\demo_python\demo\yield_demo.py", line 49, in 
    a()
  File "E:\eclipse-workspace\demo_python\demo\yield_demo.py", line 19, in a
    b_next = next(g_b)
StopIteration
----------------------

def d():
    for i in range(2):
        yield i

def b():
    yield d()
    print("b")
    yield

def a():
    g_b = b()
    print(g_b) # 
    g_d = next(g_b)
    print(g_d) # 
    v_d = next(g_d)
    print(v_d) # 0
    v_d = next(g_d)
    print(v_d) # 1
    b_next = next(g_b)
    print(b_next)
#---输出


0
1
b
None

--------------
@gen.coroutine
def gen_d():
    for i in range(10):
        raise gen.Return(i)

@gen.coroutine
def gen_b():
    v = yield gen_d()
    print("b")

@gen.coroutine
def gen_a():
    var = yield gen_b()
    print(var)

if __name__ == "__main__":
    ioloop.IOLoop.current().run_sync(gen_a)
--输出
b
None
-----------------------------
说明:方法gen_d的注解gen.coroutine去掉之后,
gen_d的返回值不再是Future类型,gen_b方法在执行以一条语句之后,
不会继续执行后面的语句
#@gen.coroutine
def gen_d():
    for i in range(10):
        raise gen.Return(i)

@gen.coroutine
def gen_b():
    v = yield gen_d()
    print("b")

@gen.coroutine
def gen_a():
    var = yield gen_b()
    print(var)

if __name__ == "__main__":
    ioloop.IOLoop.current().run_sync(gen_a)
--输出
0
 -----------------------------
yield直接返回
@gen.coroutine
def gen_d():
    for i in range(10):
        raise gen.Return(i)

@gen.coroutine
def gen_b():
    yield gen_d()

@gen.coroutine
def gen_a():
    var = yield gen_b()
    print(var)

if __name__ == "__main__":
    ioloop.IOLoop.current().run_sync(gen_a)
---
输出:None
----------------------
raise返回:
@gen.coroutine
def gen_d():
    for i in range(10):
        raise gen.Return(i)

@gen.coroutine
def gen_b():
    raise gen.Return(gen_d())

@gen.coroutine
def gen_a():
    var = yield gen_b()
    print(var)

if __name__ == "__main__":
    ioloop.IOLoop.current().run_sync(gen_a)
输出:
--------------------------------
有异步的情况下不能直接返回:
@gen.coroutine
def gen_d():
    url = "http://x.x.x.x:8060/api/v1/health"
    result = yield AsyncHTTPClient(defaults=dict(request_timeout=10)).fetch(url, method="GET")
    raise gen.Return((result.code, result.body))

@gen.coroutine
def gen_b():
    raise gen.Return((yield gen_d()))

@gen.coroutine
def gen_a():
    var = yield gen_b()
    print(var)

if __name__ == "__main__":
    ioloop.IOLoop.current().run_sync(gen_a)
输出:(200, b'ok')

 

posted on 2019-06-05 12:24 wenlin_gk 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/wenlin-gk/p/10978897.html

你可能感兴趣的:(yield与gen.coroutine)