Skip to content
Advertisement

faster processing of a for loop with put requests python

I have a json file – that i am reading and processing each element due to the api and the payload that is required.

I am looking for ways to speed this up through multi processing / concurrency – not for sure the proper approach. I think either of those would work as they are individual request updating a specific role within the API. The calls can be run concurrently without impacting the role itself.

the function that i currently have iterates through the following:

newapproach.py

JavaScript

The length of time right now is around 60 seconds to process all of the required payloads that need sent.

JavaScript

The json file that gets read in contains a number of api calls that are needed updating.

data.json

JavaScript

The old version that i was testing was something like the following and using yaml – which yaml was kind of a nightmare to manage.
oldversion.py

JavaScript

However this would complete the script – but push the role update to the background – and the script would complete and stall at the end waiting for the background to complete. still took the same amount of time just not as noticeable.

Ideally – I think multiprocessing is the route i would want to do but still not quite grasping how to make that a proper for loop and multiprocess that for loop.

OR – i am just crazy and there is a much better way to do it all – and i am currently an anti pattern.

UPDATED: so this concurrent config actually processes properly – however its still at the same speed as the other.

JavaScript

Advertisement

Answer

so concurrent.futures – is the ideal sweetness that is needed to process this. I had to do a little more testing but now a process that used to take 60 to 80 seconds depending on the server i was hitting. Now takes 10 seconds.

JavaScript

One of the key screw ups that i found in my previous attempts at this was

JavaScript

I didn’t have this line of code – properly setup or in my initial tests it didn’t exist. When i finally got this working – i was still seeing 60 seconds.

The next problem was it was in the for loop for roleprivs.items() – pulled that out of the initial for loop and was able to process much faster.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement