鱼C论坛

 找回密码
 立即注册
查看: 5318|回复: 4

[已解决]这个为什么报错呀,想了半天没有明白

[复制链接]
发表于 2023-2-11 12:51:47 | 显示全部楼层 |阅读模式
20鱼币
1`PWAV`](A@@{N`$}[KN_UV.png
import asyncio
import time

async def func1():
    print("你好啊,我是1")
    await asyncio.sleep(3)
    print("你好啊,我是1")


async def func2():
    print("你好啊,我是2")
    await asyncio.sleep(2)
    print("你好啊,我是2")


async def func3():
    print("你好啊,我是3")
    await asyncio.sleep(4)
    print("你好啊,我是3")


if __name__ == '__main__':
    f1 = func1()  # 此时的函数是异步协程函数。此时函数执行得到的是一个协程对象
    f2 = func2()
    f3 = func3()
    # asyncio.run(f1)  #协程程序运行需要asyncio模块的支持
    tasks = [
        f1, f2, f3
    ]
    t1 = time.time()
    # 一次性启动多个任务(协程)
    asyncio.run(asyncio.wait(tasks))
    t2 = time.time()
    print(t2 - t1)
最佳答案
2023-2-11 12:51:48
这个python3.11的问题,python3.11直接传一个协程对象会报错,得传 task

参考文章:
https://blog.csdn.net/m0_69082030/article/details/124327891
https://stackoverflow.com/questi ... -running-event-loop

参考代码:
import asyncio
import time

async def func1():
    print("你好啊,我是1")
    await asyncio.sleep(3)
    print("你好啊,我是1")


async def func2():
    print("你好啊,我是2")
    await asyncio.sleep(2)
    print("你好啊,我是2")


async def func3():
    print("你好啊,我是3")
    await asyncio.sleep(4)
    print("你好啊,我是3")


if __name__ == '__main__':
    f1 = func1()  # 此时的函数是异步协程函数。此时函数执行得到的是一个协程对象
    f2 = func2()
    f3 = func3()
    t1 = time.time()
    loop = asyncio.new_event_loop()
    tasks = [
        loop.create_task(i) for i in [f1, f2, f3]
    ]
    loop.run_until_complete(asyncio.wait(tasks))
    t2 = time.time()
    print(t2 - t1)

最佳答案

查看完整内容

这个python3.11的问题,python3.11直接传一个协程对象会报错,得传 task 参考文章: https://blog.csdn.net/m0_69082030/article/details/124327891 https://stackoverflow.com/questions/58774718/asyncio-in-corroutine-runtimeerror-no-running-event-loop 参考代码:
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-11 12:51:48 | 显示全部楼层    本楼为最佳答案   
这个python3.11的问题,python3.11直接传一个协程对象会报错,得传 task

参考文章:
https://blog.csdn.net/m0_69082030/article/details/124327891
https://stackoverflow.com/questi ... -running-event-loop

参考代码:
import asyncio
import time

async def func1():
    print("你好啊,我是1")
    await asyncio.sleep(3)
    print("你好啊,我是1")


async def func2():
    print("你好啊,我是2")
    await asyncio.sleep(2)
    print("你好啊,我是2")


async def func3():
    print("你好啊,我是3")
    await asyncio.sleep(4)
    print("你好啊,我是3")


if __name__ == '__main__':
    f1 = func1()  # 此时的函数是异步协程函数。此时函数执行得到的是一个协程对象
    f2 = func2()
    f3 = func3()
    t1 = time.time()
    loop = asyncio.new_event_loop()
    tasks = [
        loop.create_task(i) for i in [f1, f2, f3]
    ]
    loop.run_until_complete(asyncio.wait(tasks))
    t2 = time.time()
    print(t2 - t1)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-11 12:57:41 | 显示全部楼层
我运行没有问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-11 12:58:09 | 显示全部楼层
我这里正常运行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-2-11 13:35:49 | 显示全部楼层
实在想不明白
C:\Users\misaka\AppData\Local\Programs\Python\Python311\python.exe C:\Users\misaka\PycharmProjects\pythonProject\爬虫\第四章\协程.py 
Traceback (most recent call last):
  File "C:\Users\misaka\PycharmProjects\pythonProject\爬虫\第四章\协程.py", line 32, in <module>
    asyncio.run(asyncio.wait(tasks))
  File "C:\Users\misaka\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\misaka\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\misaka\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 650, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Users\misaka\AppData\Local\Programs\Python\Python311\Lib\asyncio\tasks.py", line 415, in wait
    raise TypeError("Passing coroutines is forbidden, use tasks explicitly.")
TypeError: Passing coroutines is forbidden, use tasks explicitly.
sys:1: RuntimeWarning: coroutine 'func1' was never awaited
sys:1: RuntimeWarning: coroutine 'func2' was never awaited
sys:1: RuntimeWarning: coroutine 'func3' was never awaited
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-16 09:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表