Home > Blockchain >  How to periodically update global variable in Python multithreading
How to periodically update global variable in Python multithreading

Time:10-17

I need to download large number of files from one vendor API which requires the token must be refreshed after one hour.

I'm able to use multithreading with Python as following code , but as I don't know how to periodically update the token after each hour in the way that all thread can use it so my code could not run longer than that time.

I very appreciate any help on this.

import concurrent.futures
access_token = refresh_token()
failed_urls = []
with concurrent.futures.ThreadPoolExecutor(max_workers=19) as executor:
     for url in executor.map(download_task, [url for id in list_urls]):
        failed_urls.append(url)

def download_task(url):
    # Code to download the file 
    # access_token is used here

def refresh_token():
    # This def will return a refresh token

CodePudding user response:

I just need to start a separated thread that periodically update the global variable as follow:

def refresh_access_token():
while True:
    if stop_threads:
        break
    lock = Lock()
    with lock:
        global access_token
        access_token = refresh_token()
    sleep(2400)

Then start it before the thread pool code:

thread = Thread(target = refresh_access_token)
thread.start()

# Thread pool code as in the question
stop_threads = True
thread.join() 
  • Related