break the function after certain time

后端 未结 5 1859
长情又很酷
长情又很酷 2020-11-27 16:34

In python, for a toy example:

for x in range(0, 3):
    # call function A(x)

I want to continue the for loop if function A takes more than

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 16:44

    The comments are correct in that you should check inside. Here is a potential solution. Note that an asynchronous function (by using a thread for example) is different from this solution. This is synchronous which means it will still run in series.

    import time
    
    for x in range(0,3):
        someFunction()
    
    def someFunction():
        start = time.time()
        while (time.time() - start < 5):
            # do your normal function
    
        return;
    

提交回复
热议问题