每n秒执行一次Threading.timer重复功能

时间:2019-05-06 04:09:39

标签: python-3.x python-multithreading

为了每隔n秒执行一次threading.timer重复功能,我遵循了question的回答。我的程序如下所示:如何每隔1秒返回c的值?

from threading import Timer
import time

class RepeatTimer(Timer):
    def run(self):
        while not self.finished.wait(self.interval):
        self.function(*self.args, **self.kwargs)
def dummyfn():
     a = 2
     b = 4
     c = a + b
     # print(msg)
     return c

timer = RepeatTimer(1, dummyfn)
print(timer)
timer.start()
time.sleep(5)
timer.cancel()

但是现在我无法从函数返回值。我该怎么办?

1 个答案:

答案 0 :(得分:0)

您是说您不能打印C的值吗?

如果遇到这种情况,请尝试使用<input id="custom-input" type="file" (change)="handleFiles($event)" multiple > handleFiles = function (fileInput: Event) { // // // // debugger;;;;; this.imageArr = ['image/jpeg', 'image/jpg', 'image/png','image/gif']; if (fileInput.target['files'][0]['size'] > 1024000) { $.growl.error({ title: 'Error', message: "File can not be larger than 1 MB" }); return; } if (!this.imageArr.includes(fileInput.target['files'][0]['type'])) { $.growl.error({ title: 'Error', message: "Invalid file format." }); return; } var file: File = fileInput.target['files'][0]; this.extention = file.name.substr((file.name.lastIndexOf('.') +1)).toLowerCase(); var reader = new FileReader(); reader.onload = this._handleReaderLoaded.bind(this); reader.readAsBinaryString(fileInput.target['files'][0]); } 而不是返回....

因为您没有在任何地方从该函数分配收益评估者。