如何解决loop.run_until_complete给出“ TypeError:需要asyncio.Future,协程或等待”
我有以下代码可以与套接字通信并从服务器接收数据,并且工作正常。但是,当程序退出时,我遇到了一些错误,并且程序没有退出,并且永远存在。
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
解决方法
问题出在代码上
loop.run_until_complete(await on_shutdown)
我替换为
loop.run_until_complete(on_shutdown())
错误消息消失了。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。