如果函数耗时太长,跳过循环? [英] Skip loop if a function is taking too long?

查看:61
本文介绍了如果函数耗时太长,跳过循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Python 代码花费的时间太长,如果花费的时间超过几秒钟,我想停止并跳过此函数的执行.

I have Python code which is taking too long, and I would like to stop and skip execution of this function if it takes longer than a few seconds.

比如我要计时的函数是:

For example, the function I want to time is:

batch_xs, batch_ys = train_loadbatch_from_lists(batch_size)

在某些情况下,此函数调用时间过长,我想取消它.

In some instances this function call takes too long, and I would like to cancel it.

我正在寻找这样的东西:

I'm looking for something like this:

if time for batch_xs, batch_ys = train_loadbatch_from_lists(batch_size) > 20 seconds:
    then skip

参考这篇文章.

我想知道如果发生超时,我将如何再次调用该函数.

I would like to know how I would call the function again if timeout occurs.

例如

@timeout(15)
def abcd(hello):
#some def 

如果超过计时器,我想再次调用该函数.

I would like to call the function again if it crosses the timer.

推荐答案

当你在同一个线程中调用一个函数时,它通常不会返回直到完成.您调用的函数实际上必须首先设计为可中断的.实现这一目标的方法有很多种,具有不同程度的复杂性和通用性.

When you call a function in the same thread, it will normally not return until complete. The function you call really has to be designed to be interruptible in the first place. There are many ways to achieve this, with varying degrees of complexity and generality.

可能最简单的方法是将时间限制传递给您的函数,并以小块处理工作.处理完每个块后,检查经过的时间是否超过超时时间,如果是,则提前保释.

Probably the simplest way is to pass the time limit to your function, and process the work in small chunks. After each chunk is processed, check if the elapsed time exceeds the timeout and if so, bail early.

下面的例子说明了这个想法,每个块的工作花费随机的时间,有时会完成有时会超时:

The following example illustrates this idea, with the work taking a random amount of time per chunk which will sometimes complete and sometimes time out:

import time
import random
import datetime

class TimeoutException(Exception):
    def __init__(self, *args, **kwargs):
        Exception.__init__(self, *args, **kwargs)

def busy_work():

    # Pretend to do something useful
    time.sleep(random.uniform(0.3, 0.6))

def train_loadbatch_from_lists(batch_size, timeout_sec):

    time_start = datetime.datetime.now()
    batch_xs = []
    batch_ys = []

    for i in range(0, batch_size+1):
        busy_work()
        batch_xs.append(i)
        batch_ys.append(i)

        time_elapsed = datetime.datetime.now() - time_start
        print 'Elapsed:', time_elapsed
        if time_elapsed > timeout_sec:
            raise TimeoutException()

    return batch_xs, batch_ys

def main():

    timeout_sec = datetime.timedelta(seconds=5)
    batch_size = 10
    try:
        print 'Processing batch'
        batch_xs, batch_ys = train_loadbatch_from_lists(batch_size, timeout_sec)
        print 'Completed successfully'
        print batch_xs, batch_ys
    except TimeoutException, e:
        print 'Timeout after processing N records'

if __name__ == '__main__':
    main()

实现此目的的另一种方法是在单独的线程中运行辅助函数,并使用 Event 允许调用者通知辅助函数应提前终止.

Another way to achieve this is to run the worker function in a separate thread, and use an Event to allow the caller to signal the worker function should terminate early.

一些帖子(例如上面链接的帖子)建议使用信号,但不幸的是,信号会导致额外的并发症,因此不推荐使用.

Some posts (such as the linked one above) suggest using signals, but unfortunately signals can cause additional complications, and so is not recommended.

这篇关于如果函数耗时太长,跳过循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆