Bring Drag & Drop to Dash with Dashboard Engine. 💫 Learn how at our next webinar!

Multithreading/processing with Python Dash

Hello! I hope you are doing well. I have an issue with understanding how to do multithreading (or multiprocessing) with my Dash server:

I need to refresh a function every minute, here is the function:

Hello! I hope you are doing well. I have an issue with understanding how to do multithreading (or multiprocessing) with my Dash server:

I need to refresh a function every minute, here is the function:

def background():
  while True:
    global list_live_games
    debut = time.time()
    list_live_games = []
    for champ in BET_URLS_DICT.values():
      elem_to_add = get_live_teams(champ)
    for elem in elem_to_add:
      list_live_games.append(elem)
    fin = time.time()
    sleepy_time = 60 - (fin - debut)
    if sleepy_time > 0:
      print(f"Going to sleep for {sleepy_time}s")
      time.sleep(sleepy_time)

And I want to launch it meanwhile my Dash server keeps refreshing constantly. The Dash platform uses the data from the function background().

Here is what I tried (following what I found on the Web because, not gonna lie, I do not really get those concepts comme multithreading):

def start_server(app, **kwargs):
    def run():
        app.run_server(debug=True, **kwargs)

b = threading.Thread(name='background', target=background)
f = threading.Thread(name='foreground', target=foreground)

b.start()
f.start()

Thanks a lot to those who took time to read this and thanks more again to those who will try to help.
Kisses!