yeild from 后面的函数应该返回什么?

0 0 python
小爷ㄆ我不在乎ツ
2018-08-08 15:17:01 提问

关注 0关注

收藏 0收藏, 1710浏览

用asyncio实现了一个协程:

import threading
import asyncio
@asyncio.coroutine
def hello():
    print('Hello world! (%s)' % threading.currentThread())
    print(1)
    注册送白菜38 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():
    注册送白菜38 返回'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:  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:  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():
    注册送白菜38 返回一个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:  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:  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():
    ??????

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

请先 登录 后评论

1个回答

  • ERP↘SAP135声望 2018-08-08 11:44

    你既然在异步框架用了协程,在hello前有异步装饰器,那么你在yiled from后面跟的这个函数必须要有框架的异步装饰器进行装饰。就像如下: @asyncio.coroutine def foo(): time.sleep(1) return "1" 想更深入地了解装饰器的作用,你就需要自己去搜搜资料学了。一般异步框架的话,只要这么装饰起来用就OK了。
    请先 登录 后评论

注册送白菜38

悬赏追问
10
  • 10
  • 20
  • 50
  • 100
  • 200
  • 输入数值
发布追问