【问题标题】:loop.run_until_complete gives "TypeError: An asyncio.Future, a coroutine or an awaitable is required"loop.run_until_complete 给出“TypeError: An asyncio.Future, a coroutine or an awaitable is required”
【发布时间】:2020-11-30 08:09:56
【问题描述】:

我有以下代码与套接字通信并从服务器接收数据并且工作正常。但是当程序退出时,我遇到了一些错误并且程序没有退出并且它永远存在。

 async def on_shutdown():
    # close peer connections  
    for pc in pcs.values():
         await pc.close()
    pcs.clear() 
    print("on shutdown exit...")

    #coros = [pc.close() for pc in pcs]
    #await asyncio.gather(*coros)
    #pcs.clear() 

    

if __name__ == "__main__":
   
    loop = asyncio.get_event_loop()
    try:
        loop.run_until_complete(signalingServerConnect(args))
   
    finally:
        # cleanup
        
        loop.run_until_complete(await on_shutdown)
        print("Exit...")

退出代码时出现以下错误。可能是什么原因。 错误:

loop.run_until_complete(on_shutdown)
  File "/usr/lib/python3.7/asyncio/base_events.py", line 563, in run_until_complete
    future = tasks.ensure_future(future, loop=self)
  File "/usr/lib/python3.7/asyncio/tasks.py", line 592, in ensure_future
    raise TypeError('An asyncio.Future, a coroutine or an awaitable is '
TypeError: An asyncio.Future, a coroutine or an awaitable is required

【问题讨论】:

    标签: python async-await


    【解决方案1】:

    问题出在代码上

        loop.run_until_complete(await on_shutdown)
    

    我换成

        loop.run_until_complete(on_shutdown())
    

    错误消息消失了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-27
      • 2016-11-17
      • 1970-01-01
      • 2019-03-11
      • 2022-01-08
      • 2018-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多