为了账号安全,请及时绑定邮箱和手机立即绑定

Python线程参数问题

Python线程参数问题

潇湘沐 2021-05-30 14:33:23
我已经尝试了大约半个小时,但似乎无法理解我在这里做错了什么。工作代码:import threadingfrom time import sleepdef printX():    threading.Timer(5.0, printX).start()    print("five")printX()while True:    print("1")    sleep(1)这有效,但是我需要能够动态分配打印语句以及延迟。所需代码: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贡献1836条经验 获得超3个赞

另一种方法是使用printX 作为内部函数定义一个类。


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)


查看完整回答
反对 回复 2021-06-01
  • 2 回答
  • 0 关注
  • 173 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号