[英]time.sleep not working as expected when adding end= to print()
我正在嘗試打印一個消息,每個字符之間的間隔為0.2秒。 我在我的print()方法中添加了一個end =“”,以便消息顯示在1行,但是當我啟動線程時,在暫停之前只打印1個字符,直到剩余的休眠時間結束並且其余的消息被打印。
程序工作正常沒有結束=“”,但我不知道為什么。
import time,threading
msg = "68 111 110 117 116 115 32 97 114 101 32 98 111 109 98 "
msg = [int(x) for x in msg.split()]
def print_msg():
for c in msg:
print(chr(c),end=""),time.sleep(0.2)
threading.Thread(target=print_msg).start()
很高興看到一個OP可以提供一個可執行的例子! 問題很簡單,stdout有一個緩存,你需要刷新結果:
import time, threading
msg = "68 111 110 117 116 115 32 97 114 101 32 98 111 109 98 "
msg = [int(x) for x in msg.split()]
def print_msg():
for c in msg:
print(chr(c), end="", flush=True)
time.sleep(0.2)
threading.Thread(target=print_msg).start()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.