我试图每秒运行三个函数(每个函数最多可能需要1秒钟的时间)。然后,我想存储每个函数的输出,并将它们写入单独的文件。每秒运行多个函数,将结果写入文件
目前我正在使用Timer
s来进行延迟处理。 (我可以继承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])
的思考?
编辑:新增我自己的答案作为一种可能性
+1这适用,因为输出是分开文件。如果它不平凡并且涉及输出的组合,那么在写入之前,您将不得不等待所有排队的项目完成。请务必在所获取的项目上调用'task_done'。 – darvids0n 2012-01-16 04:08:01
好主意!队列是线程安全的,比list.append(data)好得多 – 2012-01-16 04:08:11
我按照darvids0n的建议将调用添加到'task_done'。谢谢。 – srgerg 2012-01-16 04:18:09