Python长间隔计时器不在Windows上触发

时间:2016-09-14 17:56:04

标签: python windows

我有一个问题,出于某种原因,如果python threading.Timer有很长的间隔(在我的情况下有点超过35分钟= 2130秒),它就不会被激活。我在Windows 7 64位上使用这个python版本:

Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25:23) [MSC v.1600 64 bit (AMD64)] on win32

这是一个展示它的示例程序:

import datetime
import logging
import threading
import time

logging.basicConfig(format='%(asctime)-15s: %(message)s', level=logging.DEBUG)
log = logging.getLogger('log')

def timer(secs): 
  log.info('timer: %d secs, %.2f mins' % (secs, secs / 60))

min = 70
max = 80
mul = 30

log.info('min: %d, max: %d, mul: %s' % (min, max, mul))

for i in range(min, max):
  secs = i * mul
  t = threading.Timer(secs, timer, kwargs=dict(secs=secs))
  t.daemon = True
  t.start()

log.info('Scheduled all timers')

time.sleep((max + 1) * mul)

log.info('Exiting')

跑步时,我明白了:

2016-09-14 10:30:51,451: min: 70, max: 80, mul: 30
2016-09-14 10:30:51,452: Scheduled all timers
2016-09-14 11:05:51,459: timer: 2100 secs, 35.00 mins
2016-09-14 11:06:21,454: timer: 2130 secs, 35.50 mins
2016-09-14 11:11:21,468: Exiting

我现在没有在Linux上使用python3,但我在这里尝试过:

结果:

sh-4.3$ python3                                                                                                                                                                                                         
Python 3.4.3 (default, Jun 29 2015, 12:16:01)                                                                                                                                                                           
[GCC 5.1.1 20150618 (Red Hat 5.1.1-4)] on linux                                                                                                                                                                         
Type "help", "copyright", "credits" or "license" for more information.                                                                                                                                                  
>>>                                                                                                                                                                                                                     
sh-4.3$ python3 main.py                                                                                                                                                                                                  
2016-09-14 17:13:49,800: min: 70, max: 80, mul: 30                                                                                                                                                                       
2016-09-14 17:13:49,807: Scheduled all timers                                                                                                                                                                            
2016-09-14 17:48:49,801: timer: 2100 secs, 35.00 mins                                                                                                                                                                    
2016-09-14 17:49:19,801: timer: 2130 secs, 35.50 mins                                                                                                                                                                    
2016-09-14 17:49:49,802: timer: 2160 secs, 36.00 mins                                                                                                                                                                    
2016-09-14 17:50:19,802: timer: 2190 secs, 36.50 mins                                                                                                                                                                    
2016-09-14 17:50:49,803: timer: 2220 secs, 37.00 mins                                                                                                                                                                    
2016-09-14 17:51:19,804: timer: 2250 secs, 37.50 mins                                                                                                                                                                    
2016-09-14 17:51:49,804: timer: 2280 secs, 38.00 mins                                                                                                                                                                    
2016-09-14 17:52:19,805: timer: 2310 secs, 38.50 mins                                                                                                                                                                    
2016-09-14 17:52:49,806: timer: 2340 secs, 39.00 mins                                                                                                                                                                    
2016-09-14 17:53:19,807: timer: 2370 secs, 39.50 mins                                                                                                                                                                    
2016-09-14 17:54:19,900: Exiting  

并且您可以看到它按预期工作。

我也尝试过在Linux上使用python2,但它运行良好。我尝试在Windows上使用Cygwin的python2,它也运行良好:

$ python
Python 2.7.10 (default, Jun  1 2015, 18:05:38)
[GCC 4.9.2] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
$ python timer.py
2016-09-14 12:48:56,606: min: 70, max: 80, mul: 30
2016-09-14 12:48:56,622: Scheduled all timers
2016-09-14 13:23:56,615: timer: 2100 secs, 35.00 mins
2016-09-14 13:24:26,614: timer: 2130 secs, 35.00 mins
2016-09-14 13:24:56,613: timer: 2160 secs, 36.00 mins
2016-09-14 13:25:26,612: timer: 2190 secs, 36.00 mins
2016-09-14 13:25:56,611: timer: 2220 secs, 37.00 mins
2016-09-14 13:26:26,610: timer: 2250 secs, 37.00 mins
2016-09-14 13:26:56,624: timer: 2280 secs, 38.00 mins
2016-09-14 13:27:26,623: timer: 2310 secs, 38.00 mins
2016-09-14 13:27:56,622: timer: 2340 secs, 39.00 mins
2016-09-14 13:28:26,636: timer: 2370 secs, 39.00 mins
2016-09-14 13:29:26,634: Exiting

任何人都有类似的经历吗?如果你有一台带有python3的Windows机器,你可以运行它,看看它是否以同样的方式运行在你的终端上?

0 个答案:

没有答案