Python Migrating Single Threaded Function to a Multi Threaded function

This post is a follow-up to the post I created that gives a basic multi threaded example. There isn’t any context and when I came back to the code I found it difficult to translate into one of my functions.

In this example, I will take a function of my F5 Flask Dashboard that has no multi threading and make it use another function that will handle the multi threading.
The background to this is to run three API requests to each F5. I am looking for details of the device version and failover stats and status. Currently, each F5 must run each of these APIs before moving on to the next API and so on. Some of the F5s take a while to respond and hold the process up. Using a multi threaded approach means that the F5s that take a while to respond can be in their waiting state while the responsive F5s can return the data and another F5 can join the multi threading queue.

I am currently running this as a test against 24 F5s. Each API is still performed in sequence for each F5. A second version of this will be to send a number of APIs, so a new connection isn’t required to further speed things up. However, in the current state, the time is a big difference.
– Before the multi threading was added: 234.580 seconds.
– Single thread (using the multi threaded functions): 246.64 seconds
– 16 threads: 44.57 seconds

Original Version

Flask Function

This is the function that kicks it all off. Here are the three API calls. Each is calling the exact same function, just passing in a different path.

Create API Request Function

This is the function that will be modified to move into a multi threaded state. Currently, it just loops through each F5 IP and path and passes that to the actual F5 function that will make all the F5 calls for the project.

F5 API Function

This is the last function of this journey, to actually send the API request to each F5. The multi threading will essentially be calling multiple instances of this function.

Multi Threaded Version

As I said, the only changes made are to the Create API Request Function. The process of this is to follow the same procedure, as the original, by having the flask function send a list of IPs and a path to make the API call. This function is now split into two parts.
– Function get_f5_info to take in the IP list, path and start the worker thread
– Function get_f5_info_MT is the worker thread, this take the IP and path and calls the API send function

What will happen
1. The function named get_f5_info takes in the F5 IP list and path (same as earlier)
2. The function named get_f5_info starts worker threads for each IP
3. The function named get_f5_info_MT will make the API request for each worker thread created until IP list is completed
3. Once completed, a list is returned
4. Process repeats, the next API path is sent with the IP list

The get_f5_info function is creating worker threads that call the multi threading function to make the API requests.

The multi threading function will make the call to the API