簡體   English   中英

Python在n秒后停止線程

[英]Python stop a thread after n seconds

5秒后如何停止線程? 我想每秒鍾打印一次“ Hello World”,然后在5秒鍾后停止。 這是我的代碼:

import threading

def printthis():
   threading.Timer(1.0, printthis).start()
   print("Hello, World!")

printthis()

這個怎么樣:

def printthis():
    print_count = 0
    while print_count < 5: # 5 because print_count will be 5 after printing the 5th time
        threading.Timer(1.0, printthis).start()
        print("Hello, World!")
        print_count += 1 
printthis()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM