首页 > 解决方案 > Python线程参数问题

问题描述

我已经尝试了大约半个小时,但似乎无法理解我在这里做错了什么。工作代码:

import threading
from time import sleep


def printX():
    threading.Timer(5.0, printX).start()
    print("five")


printX()
while True:
    print("1")
    sleep(1)

这可行,但是我需要能够动态分配打印语句以及延迟。所需代码:

import threading
from time import sleep


def 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)

提前感谢您的帮助:)。

标签: pythonpython-3.xmultithreadingparametersparallel-processing

解决方案


threading.Timer可以传递参数args

threading.Timer(int(time), printX, (time, message)).start()

阅读更多关于它的文档


推荐阅读