账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    yeild from 后面的函数应该返回什么?
    35
    0

    用asyncio实现了一个协程:

    import threading
    import asyncio
    
    @asyncio.coroutine
    def hello():
        print('Hello world! (%s)' % threading.currentThread())
        print(1)
        # asyncio.sleep(1)返回None
        r = yield from asyncio.sleep(1)
        print('Hello again! (%s)' % threading.currentThread())
        print(2)
    
    loop = asyncio.get_event_loop()
    tasks = [hello(), hello()]
    loop.run_until_complete(asyncio.wait(tasks))
    loop.close()
    

    输出:

    Hello world! (<_MainThread(MainThread, started 6404)>)
    1
    Hello world! (<_MainThread(MainThread, started 6404)>)
    1
    Hello again! (<_MainThread(MainThread, started 6404)>)
    2
    Hello again! (<_MainThread(MainThread, started 6404)>)
    2
    

    可以看到是同一个进程运行了两个协程,并且asyncio.sleep(1)这个函数模拟了一个一秒的IO等待,这就很迷了,我理解的是,程序运行到yeild from 就运行asyncio.sleep(1)这个函数,然后等待IO返回,并去运行了另外一个hello(),待asyncio.sleep(1)运行完毕又接着运行hello()接下来的代码
    我就想了:

    **可不可以自己写个asyncio.sleep(1)函数啊,返回自己定义的值,赋值给r?**

    但是并不是我想的那么简单,报错了,我写的如下:

    import threading
    import asyncio
    import time
    
    def foo():
        # 返回'1'
        time.sleep(1)
        return '1'
    
    @asyncio.coroutine
    def hello():
        print('Hello world! (%s)' % threading.currentThread())
        print(1)
        r = yield from foo()
        print('Hello again! (%s)' % threading.currentThread())
        print(2)
    
    loop = asyncio.get_event_loop()
    tasks = [hello(), hello()]
    loop.run_until_complete(asyncio.wait(tasks))
    loop.close()

    报错如下:

    Hello world! (<_MainThread(MainThread, started 6832)>)
    1
    Hello world! (<_MainThread(MainThread, started 6832)>)
    1
    Task exception was never retrieved
    future: <Task finished coro=<hello() done, defined at test3.py:12> exception=RuntimeError("Task got bad yield: '1'",)>
    Traceback (most recent call last):
      File "test3.py", line 16, in hello
        r = yield from foo()
    RuntimeError: Task got bad yield: '1'
    Task exception was never retrieved
    future: <Task finished coro=<hello() done, defined at test3.py:12> exception=RuntimeError("Task got bad yield: '1'",)>
    Traceback (most recent call last):
      File "test3.py", line 16, in hello
        r = yield from foo()
    RuntimeError: Task got bad yield: '1'
        TypeError: 'int' object is not iterable
    

    看提示是要返回一个iterable,改了一下:

    def foo():
        # 返回一个iter
        time.sleep(1)
        return iter('1')

    报错:

    Hello world! (<_MainThread(MainThread, started 6576)>)
    1
    Hello world! (<_MainThread(MainThread, started 6576)>)
    1
    Task exception was never retrieved
    future: <Task finished coro=<hello() done, defined at test3.py:12> exception=RuntimeError("Task got bad yield: '1'",)>
    Traceback (most recent call last):
      File "test3.py", line 16, in hello
        r = yield from foo()
    RuntimeError: Task got bad yield: '1'
    Task exception was never retrieved
    future: <Task finished coro=<hello() done, defined at test3.py:12> exception=RuntimeError("Task got bad yield: '1'",)>
    Traceback (most recent call last):
      File "test3.py", line 16, in hello
        r = yield from foo()
    RuntimeError: Task got bad yield: '1'

    看不懂RuntimeError: Task got bad yield: '1',求大佬解释

    帮我写个

    def foo():
        ??????

    要是我的理解有错也欢迎指出,谢谢各位

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 無人相依偎 普通会员 1楼

      根据上下文,我不知道"yeild from 后面的函数应该返回什么"的具体含义是什么。如果你能提供更多的信息,我会尽力回答你的问题。

    更多回答
    网站公告
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部