擅长:python、mysql、java
<p><code>asyncio</code>似乎并不特别适合这个问题。您可能需要简单的线程:</p>
<p>其原因是,当父任务完成时,您的任务被终止。通过抛出一个<code>daemon</code>线程,您的任务将继续运行,直到它完成,或者直到程序退出。你知道吗</p>
<pre class="lang-py prettyprint-override"><code>import threading
import time
def Slowpoke():
try:
print("I see you shiver with antici...")
time.sleep(3)
print("...pation!")
except:
print("Yup")
raise Exception()
def main():
task = threading.Thread(target=Slowpoke)
task.daemon = True
task.start()
return "Hi!"
if __name__ == "__main__":
print(main())
while True:
pass
</code></pre>