在main中调用一次时,Python线程运行两次

问题描述 投票:5回答:1
if __name__ == '__main__':

    t = threading.Thread(target = authtarget)
    t.daemon = True
    t.start()
    print 'running thread'
    app.run(debug=True)

这个主要在我们的服务器中,app.run将启动服务器并能够处理请求。我们创建的线程是一个定时器,每5秒检查一次if语句。但是,t.start()将创建两个线程而不是一个线程。我们已经尝试将t.start()更改为t.run()但是当我们这样做时,我们永远不会访问app.run,我们需要运行服务器。

def authtarget():
    sp  = Spotify()
    db = Database()
    resultList = []
    while True:
        time.sleep(5)
        sp.timer(204) 

timer()是我们需要每5秒调用一次的函数。但是,使用我们当前的代码,计时器被调用两次而不是每5秒调用一次

python multithreading flask bottle
1个回答
5
投票

谢谢大家,我刚刚将app.run(debug = True)更改为app.run(debug = False),以便它不会运行两次

© www.soinside.com 2019 - 2024. All rights reserved.