Search code examples
pythonconcurrency

Concurrency with a REST API that uses JWT


I am designing a solution that will perform concurrent http requests to certain REST API. Thing is that this API requires to make a POST request to an authentication endpoint (with username + api_key in the header) so that the server gives you a 20min valid JWT.

As far as I see whenever you ask for a new token, previous token is no longer valid.

In this scenario, is concurrency a possibility? (e.g. using multi-thread in python). As far as I understand, with that principle of working with JWT only one thread shall be doing the job, and not "n" number of threads concurrently, as each thread would invalidate previous token

enter image description here


Solution

  • Create one thread which only manages the authentication, ie fetches a new JWT every 15 minutes (something under 20).

    Create N worker threads which make the POST requests.

    All threads should share a variable holding the JWT in use and a synchronization primitive like threading.Event. Where you want to store them (global, class, etc) is up to you.

    The workers wait for this Event to be set via ev.wait() before every POST request. The auth thread clear the event via ev.clear() when it needs to fetch a new JWT and when it has set the JWT variable, just set the Event again via ev.set()

    This way the POST workers will run freely until the time to refresh the JWT, then pause only while a new one is being fetched.

    This can also be done with asyncio.Event if you use async concurrency.