抱歉,这是另一个问题的欺骗。
asyncio.create_task()
已在此处发布了替代品。
这是使用替换后的输出create_task()
案例一:有await self.task
$ uvicorn test:app
INFO: Started server process [33213]
INFO: Waiting for application startup.
ERROR: Traceback (most recent call last):
File "/Users/dev/.virtualenvs/server-99338def/lib/python3.8/site-packages/starlette/routing.py", line 517, in lifespan
await self.startup()
File "/Users/dev/.virtualenvs/server-99338def/lib/python3.8/site-packages/starlette/routing.py", line 494, in startup
await handler()
File "./test.py", line 35, in main
await MyTasks().main()
File "./test.py", line 27, in main
print(await self.task)
File "./test.py", line 13, in wrapper
return await task
File "./test.py", line 30, in hello
raise ValueError
ValueError
ERROR: Application startup failed. Exiting.
案例2:没有await self.task
$ uvicorn test:app
INFO: Started server process [32627]
INFO: Waiting for application startup.
INFO: Application startup complete.
ERROR: Exception in callback <function create_task.<locals>.on_done at 0x10c519550>
handle: <Handle create_task.<locals>.on_done created at /Users/dev/.virtualenvs/server-99338def/lib/python3.8/site-packages/uvicorn/main.py:382>
source_traceback: Object created at (most recent call last):
File "/Users/dev/.virtualenvs/server-99338def/bin/uvicorn", line 8, in <module>
sys.exit(main())
File "/Users/dev/.virtualenvs/server-99338def/lib/python3.8/site-packages/click/core.py", line 764, in __call__
return self.main(*args, **kwargs)
File "/Users/dev/.virtualenvs/server-99338def/lib/python3.8/site-packages/click/core.py", line 717, in main
rv = self.invoke(ctx)
File "/Users/dev/.virtualenvs/server-99338def/lib/python3.8/site-packages/click/core.py", line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/Users/dev/.virtualenvs/server-99338def/lib/python3.8/site-packages/click/core.py", line 555, in invoke
return callback(*args, **kwargs)
File "/Users/dev/.virtualenvs/server-99338def/lib/python3.8/site-packages/uvicorn/main.py", line 331, in main
run(**kwargs)
File "/Users/dev/.virtualenvs/server-99338def/lib/python3.8/site-packages/uvicorn/main.py", line 354, in run
server.run()
File "/Users/dev/.virtualenvs/server-99338def/lib/python3.8/site-packages/uvicorn/main.py", line 382, in run
loop.run_until_complete(self.serve(sockets=sockets))
Traceback (most recent call last):
File "uvloop/cbhandles.pyx", line 70, in uvloop.loop.Handle._run
File "./test.py", line 9, in on_done
fut.result()
File "./test.py", line 30, in hello
raise ValueError
ValueError