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

Bob*_* Me 7 python multithreading bottle flask

if __name__ == '__main__':

    t = threading.Thread(target = authtarget)
    t.daemon = True
    t.start()
    print 'running thread'
    app.run(debug=True)
Run Code Online (Sandbox Code Playgroud)

这个主目录位于我们的服务器中,其中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) 
Run Code Online (Sandbox Code Playgroud)

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

Bob*_* Me 8

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