16
if __name__ == '__main__':

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

This main is in our server, where app.run will start the server and will be able to handle requests. The thread we are making is a timer that checks a certain if statement every 5 seconds. However, t.start() will create two threads instead of only one. We've tried changing t.start() to t.run() however when we do that we never get to app.run, which we need to run the server.

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

timer() is a function that we need called every 5 seconds. However, with the code that we have currently, timer gets called twice instead of once every 5 seconds

davidism
  • 121,510
  • 29
  • 395
  • 339
  • 1
    a [mcve] would be nice. – Jean-François Fabre Apr 26 '17 at 20:40
  • 1
    Following @Jean-FrançoisFabre's comment, what does `app.run()` do? That looks like the obvious suspect for a second thread of execution. – Don Kirkby Apr 26 '17 at 20:41
  • app.run() just runs some auxillary functions for the main part of the program. It does not have any thread calls though. – Bob Marley and Me Apr 26 '17 at 20:44
  • 1
    Is 'app' defined as an object from an external library, (i.e. Flask)? On, Flask for example, app.run with a debug flag may cause the app to load twice. If that's the case, a possible solution is to set an environment variable (using os.environ) when the thread starts, and quitting if it's already set so that the thread won't run twice. – eshirazi Apr 26 '17 at 21:07

1 Answers1

25

I just changed

app.run(debug=True)

to

app.run(debug=False) 

so that it didn't run twice

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219