【发布时间】:2018-02-26 16:59:29
【问题描述】:
所以我有一个倒计时脚本,看起来像这样:
import time, threading, asyncio
def countdown(n, m):
print("timer start")
time.sleep(n)
print("timer stop")
yield coro1
async def coro1():
print("coroutine called")
async def coromain():
print("first")
t1 = threading.Thread(target=countdown, args=(5, 0))
t1.start()
print("second")
loop = asyncio.get_event_loop()
loop.run_until_complete(coromain())
loop.stop()
我想要它做的很简单:
Run coromain
Print "first"
Start thread t1, print "timer start" and have it wait for 5 seconds
In the mean time, print "second"
after 5 seconds, print "timer stop"
exit
但是,当我运行此代码时,它会输出:
Run coromain
Print "first"
Print "second"
exit
我很困惑为什么会这样。谁能解释我在这里做错了什么?
【问题讨论】:
-
你能解释一下在不同线程中运行倒计时的选择吗?
-
我正在制作一个不和谐的机器人。我需要它倒计时 n 秒然后通知用户,同时仍然能够在主线程中接受命令
标签: python multithreading python-3.6 python-asyncio