- 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 条
- 全部回答
更多回答
网站公告
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部

