我已經(jīng)嘗試了大約半個小時,但似乎無法理解我在這里做錯了什么。工作代碼:import threadingfrom time import sleepdef printX(): threading.Timer(5.0, printX).start() print("five")printX()while True: print("1") sleep(1)這有效,但是我需要能夠動態(tài)分配打印語句以及延遲。所需代碼:import threadingfrom time import sleepdef printX(time, message): threading.Timer(int(time), printX).start() print(str(message)printX(time, message)while True: print("Rest of the program continues") sleep(1)感謝您的任何幫助:)。
2 回答

米脂
TA貢獻(xiàn)1836條經(jīng)驗 獲得超3個贊
另一種方法是使用printX 作為內(nèi)部函數(shù)定義一個類。
class thread:
def __init__(self, time, message):
self.time = time
self.message = message
def printX(self):
threading.Timer(int(self.time), self.printX).start()
print(str(self.message))
thread(3,"test message").printX()
while True:
print("Rest of the program continues")
sleep(1)
添加回答
舉報
0/150
提交
取消