在多种情况下调用Python方法的最佳方法

问题描述 投票:0回答:2

我有一个工作流程,在以下任何一种情况下,我都需要调用python方法:1.指定的超时发生,或2.输入数据(列表)的大小达到阈值,例如10个数据点

支持工作流程的最佳方法是什么?

[编辑]-这些方法将在无服务器API中调用,因此它必须是无状态的。使用某种队列来存储和检索数据是否有意义,以及如何使用?

python queue python-multithreading
2个回答
0
投票

您可以这样做:

while True: #Keep checking condition
    if timeoutCondition or len([list]) > 10: #check conditions
        class.method() #execute method

0
投票

您可以按一定的时间间隔拉状态,如果超时或数据达到阈值,请执行所需的操作。

import time

max_count = 10
timeout = 60 #1min

count_increase = 0
next_timeout = time.time() + timeout

while True:
    current_time = time.time
    count_increase = get_count() - count
    if count >= max_count or current_time >= next_timeout:
        do_what_you_want()
        count_increase = 0
        next_timeout = time.time() + timeout

    time.sleep(1)
© www.soinside.com 2019 - 2024. All rights reserved.