线程不更新模块时间

问题描述 投票:0回答:1

我是python的新手,我写了一个简单的代码来按顺序运行,并在触发时发送电子邮件。我使用线程计时器来计数和导入outbound.py(带有导入日期时间)只是为了清理主脚本,因此它在main.py中不会包含电子邮件脚本。但是,当收到第一封电子邮件时,消息时间可以正确反映,但是下一封电子邮件与第一封电子邮件保持相同的时间。

下面是main.py脚本

import threading
from outbound import outbound

def run ():
    threading.Timer(10,run).start()
    outbound()   
run()

outbound.py脚本

from datetime import datetime   
now=datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")

def outbound():    
   print (dt_string)
python-3.x python-import python-multithreading
1个回答
0
投票

导入模块时创建了dt_string,从此不再对其进行更新。将字符串初始化移到函数主体中。

def outbound():    
    dt_string = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
    print (dt_string)
© www.soinside.com 2019 - 2024. All rights reserved.