python 文本文件中按每秒记录数_python – 每秒运行多个函数,将结果写入文件

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 20:19   1916   0

我试图每秒运行三个函数(每个函数最多需要1秒执行).然后,我想存储每个函数的输出,并将它们写入单独的文件.

目前我正在使用Timers进行延迟处理. (我可以将Thread子类化,但这对于这个简单的脚本来说有点复杂)

def main:

for i in range(3):

set_up_function(i)

t = Timer(1, run_function, [i])

t.start()

time.sleep(100) # Without this, main thread exits

def run_function(i):

t = Timer(1, run_function, [i])

t.start()

print function_with_delay(i)

处理function_with_delay输出的最佳方法是什么?将结果附加到每个函数的全局列表中?

然后我可以在我的主函数结束时放这样的东西:

...

while True:

time.sleep(30) # or in a try/except with a loop of 1 second sleeps so I can interrupt

for i in range(3):

save_to_disk(data[i])

思考?

编辑:添加我自己的答案作为可能性

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP