read write lock with retry in python

107 Views Asked by At

I have many task that i want to solve with a threadpool and use a access_token which i want to have it as share variable. this access_token expire and i request a new one from server and update the value.

So i want only one thread to write to object and many read and when the write lock is on other should wait on read and others that have acquired the access token be stopped if network request retry so they work with new token. read-write locks do not provide this specific function as of my knowledge

**threadpool

    with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
        for inx, i in enumerate(inputs):
            executor.submit(worker, solve_asset, recover, i, inx)

request


response = make_request_with_retry(self.url,
                                           headers=headers
                                           )

if response.code == 200:

   data = json.loads(response)
   save_json_on_disk(response_text)

elif response.code == 401:
   with lock:#?
   print("Access token expired. Trying to refresh it...")
   # Perform token refresh and update the headers with new access token
   new_token = send_access_key_refresh()  # Replace refresh_token() with your token refresh logic
   headers['Authorization'] = 'Bearer ' + new_token
            ```
0

There are 0 best solutions below