【问题标题】:python stop function with very short timeout超时时间很短的python停止功能
【发布时间】:2017-12-15 13:27:59
【问题描述】:

我需要将一个函数应用于一个大列表,例如[funz(x) for x in my_list]

每次评估都必须非常迅速。假设funz(x) 应该在不到0.1 秒内执行。

如果执行时间超过0.1 秒,我希望我的funz 返回None

实际上,如果 my_listn 元素。代码应该在不到0.1*n 秒内执行。

最好的方法是什么?

【问题讨论】:

  • 您的意思是在最多 0.1 秒后返回 None,或者如果耗时太长,则为函数计时并返回 None
  • 在最多 0.1 秒后返回无。
  • 所以我的清单很长。我想为大多数元素设置funz(x) 的值,但我需要它在不太长的时间内完成
  • 如果你的函数中有一个循环,可能会不时检查经过的时间,如果超过则返回。取决于你的功能是什么。也许你可以模拟它并编辑你的 q。
  • 我的函数是来自标准库的socket.getfqdn。对于某些ips,它非常慢,而对于另一些ips,它非常快。基本上我需要跳过慢 ips

标签: python python-2.7 timer signals


【解决方案1】:

这是我目前的解决方案。我不知道这是否是解决问题的最有效方法:

def ip2host(ip, return_dict):                                                                                                                                                                                      
    return_dict[ip] = socket.getfqdn(ip)                                                                                                                                                                           
    return                                                                                                                                                                                                         


def ips2hosts(ips, timeout=1):                                                                                                                                                                                     
    ips = set(ips)                                                                                                                                                                                                 
    manager = multiprocessing.Manager()                                                                                                                                                                            
    return_dict = manager.dict()                                                                                                                                                                                   

    for i, ip in enumerate(ips):                                                                                                                                                                                   
        if i % 100 == 0:                                                                                                                                                                                           
            print i, ' out of ', len(ips), " N:", len(return_dict.values()), ' Time:', time.time()                                                                                                                 
        p = multiprocessing.Process(target=ip2host, args=(ip, return_dict))                                                                                                                                        
        p.start()                                                                                                                                                                                                  
        r = p.join(timeout)                                                                                                                                                                                        
        # If thread is still active                                                                                                                                                                                
        if p.is_alive():                                                                                                                                                                                           
            p.terminate()                                                                                                                                                                                          
            p.join()                                                                                                                                                                                               
    return return_dict                                                                                                                                                                                             


d = ips2hosts(df['src_ip'].unique(), timeout=0.1)                                                                                                                                                                  

这是stdout

0  out of  12986  N: 0  Time: 1513350796.71                                                                                                                                                                        
100  out of  12986  N: 99  Time: 1513350808.22                                                                                                                                                                     
200  out of  12986  N: 190  Time: 1513350819.22                                                                                                                                                                    
300  out of  12986  N: 283  Time: 1513350830.14                                                                                                                                                                    
400  out of  12986  N: 379  Time: 1513350840.73                                                                                                                                                                    
500  out of  12986  N: 474  Time: 1513350851.43                                                                                                                                                                    
600  out of  12986  N: 569  Time: 1513350861.35                                                                                                                                                                    
700  out of  12986  N: 657  Time: 1513350874.25                                                                                                                                                                    
800  out of  12986  N: 742  Time: 1513350886.62                                                                                                                                                                    
900  out of  12986  N: 815  Time: 1513350901.21                                                                                                                                                                    
1000  out of  12986  N: 894  Time: 1513350914.35                                                                                                                                                                   
1100  out of  12986  N: 969  Time: 1513350927.44                                                                                                                                                                   
1200  out of  12986  N: 1060  Time: 1513350939.92                                                                                                                                                                  
1300  out of  12986  N: 1151  Time: 1513350952.52                                                                                                                                                                  
1400  out of  12986  N: 1243  Time: 1513350966.56                                                                                                                                                                  
1500  out of  12986  N: 1337  Time: 1513350979.73                                                                                                                                                                  
1600  out of  12986  N: 1426  Time: 1513350993.5                                                                                                                                                                   
1700  out of  12986  N: 1520  Time: 1513351006.6                                                                                                                                                                   
1800  out of  12986  N: 1613  Time: 1513351017.97                                                                                                                                                                  
1900  out of  12986  N: 1697  Time: 1513351028.07                                                                                                                                                                  
2000  out of  12986  N: 1780  Time: 1513351037.6                                                                                                                                                                   
2100  out of  12986  N: 1868  Time: 1513351046.74                                                                                                                                                                  
2200  out of  12986  N: 1961  Time: 1513351055.68                                                                                                                                                                  
2300  out of  12986  N: 2044  Time: 1513351066.37                                                                                                                                                                  
2400  out of  12986  N: 2135  Time: 1513351080.32                                                                                                                                                                  
2500  out of  12986  N: 2216  Time: 1513351095.13                                                                                                                                                                  
2600  out of  12986  N: 2312  Time: 1513351107.42                                                                                                                                                                  
2700  out of  12986  N: 2406  Time: 1513351120.16                                                                                                                                                                  
2800  out of  12986  N: 2491  Time: 1513351135.09                                                                                                                                                                  
2900  out of  12986  N: 2565  Time: 1513351149.7                                                                                                                                                                   
3000  out of  12986  N: 2634  Time: 1513351164.28                                                                                                                                                                  
3100  out of  12986  N: 2721  Time: 1513351177.83                                                                                                                                                                  
3200  out of  12986  N: 2806  Time: 1513351191.08                                                                                                                                                                  
3300  out of  12986  N: 2885  Time: 1513351205.0                                                                                                                                                                   
3400  out of  12986  N: 2965  Time: 1513351219.65                                                                                                                                                                  
3500  out of  12986  N: 3056  Time: 1513351232.24    

处理 100 ips 似乎需要大约 10 秒,即使有时它会慢一些。可能是由于所有计算都需要开销

【讨论】:

    猜你喜欢
    • 2016-07-23
    • 2018-07-04
    • 1970-01-01
    • 2014-02-27
    • 2011-04-29
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    相关资源
    最近更新 更多