Python长时间间隔计时器不在Windows上启动

2024-03-28 08:41:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我遇到了一个问题,由于某种原因,如果python threading.Timer有很长的间隔(在我的例子中,有点超过35分钟=2130秒),它就不会启动。我使用的是64位Windows 7,具有以下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机器,你能运行它,看看它在你的电脑上是否也能以同样的方式工作吗


Tags: importinfologloggingallminmaxpython3