在比较datetime之后,代码会在一段时间后挂断

时间:2014-01-05 20:27:26

标签: datetime python-2.7

我有以下代码,在比较datetime后会在一段时间后挂起。以下是我的代码。

def worker():
flag = True
while True:
    s1 = '101:35:00'
    e1 = '101:36:00'
    s2 = '101:37:00'
    e2 = '101:38:00'
    s3 = '101:39:00'
    e3 = '101:40:00'

    if int(s1[0]) == 1:
        start1 = str(datetime.datetime.now().date()) + ' ' + s1[1:9]
    else:
        start1 = str(datetime.datetime.now().date() + datetime.timedelta(days=1)) + ' ' + s1[1:9]

    if int(s2[0]) == 1:
        start2 = str(datetime.datetime.now().date()) + ' ' + s2[1:9]
    else:
        start2 = str(datetime.datetime.now().date() + datetime.timedelta(days=1)) + ' ' + s2[1:9]

    if int(s3[0]) == 1:
        start3 = str(datetime.datetime.now().date()) + ' ' + s3[1:9]
    else:
        start3 = str(datetime.datetime.now().date() + datetime.timedelta(days=1)) + ' ' + s3[1:9]

    if int(e1[0]) == 1:
        end1 = str(datetime.datetime.now().date()) + ' ' + e1[1:9]
    else:
        end1 = str(datetime.datetime.now().date() + datetime.timedelta(days=1)) + ' ' + e1[1:9]

    if int(e2[0]) == 1:
        end2 = str(datetime.datetime.now().date()) + ' ' + e2[1:9]
    else:
        end2 = str(datetime.datetime.now().date() + datetime.timedelta(days=1)) + ' ' + e2[1:9]

    if int(e3[0]) == 1:
        end3 = str(datetime.datetime.now().date()) + ' ' + e3[1:9]
    else:
        end3 = str(datetime.datetime.now().date() + datetime.timedelta(days=1)) + ' ' + e3[1:9]

    s1t = datetime.datetime.strptime(start1, '%Y-%m-%d %H:%M:%S')
    s2t = datetime.datetime.strptime(start2, '%Y-%m-%d %H:%M:%S')
    s3t = datetime.datetime.strptime(start3, '%Y-%m-%d %H:%M:%S')
    e1t = datetime.datetime.strptime(end1, '%Y-%m-%d %H:%M:%S')
    e2t = datetime.datetime.strptime(end2, '%Y-%m-%d %H:%M:%S')
    e3t = datetime.datetime.strptime(end3, '%Y-%m-%d %H:%M:%S')

    cur = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    curr = datetime.datetime.strptime(cur, '%Y-%m-%d %H:%M:%S')

    if e1t < curr <= s2t or e2t < curr <= s3t:
        flag = False

    else: pass

    while not flag:
        print 'iwashereghgj'
        print str(curr) #line1
        if s2t <= curr < e2t or s3t <= curr < e3t:          
            q1.put(True)

            flag = True
        else:
            flag = True

例如,在这种情况下,代码在curr到达第1行的'2014-01-06 01:37:00'后挂起。任何人都可以帮我解释为什么会这样发生?

1 个答案:

答案 0 :(得分:3)

所有这些重复都会使您的代码很难调试。从一些封装开始:

def process_time(timestring):
    today = datetime.date.today()
    tomorrow = today + datetime.timedelta(days=1)
    h, m, s = map(int, timestring[1:9].split(':'))
    time = datetime.time(hour=h, minute=m, second=s)
    if timestring[0] == '1':
        return datetime.datetime.combine(today, time)
    return datetime.datetime.combine(tomorrow, time)

现在很清楚该功能实际上在做什么,你可以直接跳到

s1t = process_time(s1) # and so on

另外,感觉你可以缩短

cur = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
curr = datetime.datetime.strptime(cur, '%Y-%m-%d %H:%M:%S')

curr = datetime.datetime.now()

至于你的问题,你不是在你的循环中更新curr,所以它仍然是你开始执行任务的时间。您需要继续检查当前时间。

实施:

# process time strings once, outside loop
timeslots = [(s1t, e1t), ...]
while True:
##    starting = True
    while any(s <= datetime.datetime.now() < e for s, e in timeslots):
##        if starting:
##            print("Starting:", datetime.datetime.now())
##            starting = False
        q1.put(True) # do task in timeslots
    if all(datetime.datetime.now() >= e for s, e in timeslots):
##        print("All timeslots done:", datetime.datetime.now())
        break
# whatever happens afterwards

注意:以##开头的行是出于演示目的,请注释掉“生产”用途。