Turbocharge your Infoblox RESTful API calls (half 4)

Turbocharge your Infoblox RESTful API calls (part 4)

Partially 4 of Turbocharge your Infoblox RESTful API calls sequence, we focus on what asynchronous programming is, and the way it may be applied to dramatically enhance the velocity of automation scripts.


On this fourth and ultimate article of the Turbocharge your Infoblox RESTful API calls sequence, we clarify what async programming is, its professionals and cons, in addition to show how how it may be leveraged to vastly enhance the efficiency of scripts which make use of the Infoblox WAPI. On this article we’ll once more take our authentic WAPI script which inserts 1,024 IPv4 community objects into the grid, and refactor it right into a script which leverages async programming. For this, we’ll use the aiohttp and asyncio Python libraries to make async calls to the WAPI.

NOTE: In case you are a daily consumer of the infoblox-client Python consumer wrapper to the WAPI, you won’t be able to leverage async calls since it isn’t supported by the software program. There may be an open request to have that added, however on the time this text was written, no help for async had been added.

The code utilized in growth of this text sequence is accessible at https://github.com/ddiguru/turbocharge_infoblox_api

Async Programming

Asynchronous programming is the other of synchronous programming. A synchronous program has a one-track thoughts. The stream of a synchronous script is step-wise. It performs every step (job, methodology or operate) separately and completes it earlier than transferring on to the following step or chunk of information. Within the final article, we used batch processing by stuffing quite a few community objects right into a single request WAPI name, this too is an instance of a synchronous script.

An async program or script behaves otherwise. It nonetheless makes use of a single thread of execution at a time, however the distinction is that the system doesn’t must essentially watch for the step (job, methodology, or operate) to be accomplished earlier than transferring on to the following one. An async script can successfully launch a number of duties in succession with out having to attend for responses earlier than beginning others. This system is aware of what to do when a beforehand launched job does end operating.


  • async applications are usually written to be non-blocking
  • tremendous quick!


  • extra advanced to put in writing
  • requires particular async variations of libraries, i.e. it isn’t supported in infoblox-client
  • poorly written duties can block, delay or starve excellent duties that want operating

Since we’re loading 1,024 networks, we need to achieve this as rapidly as potential. Our enterprise logic is easy and really effectively fitted to async programming, as a result of we successfully need to “shove it in there”. Our script doesn’t have any dependencies that must be solved between every community. It’s actually “finest effort” – attempt to get all of them inserted/created and perhaps output to a log any that fail.

Recall the unique synchronous script logic for including networks was as follows:

    # create IPv4 Community object(s) separately
    for community in networks:
        payload = dict(community=str(community), remark='test-network')
        s.publish(f'{url}/community', knowledge=json.dumps(payload), confirm=False)

We refactored the above script to make use of asynchronous logic for including networks as follows:

    auth = aiohttp.BasicAuth(login=ibx_username, password=ibx_password)
    async with aiohttp.ClientSession(cookie_jar=aiohttp.CookieJar(unsafe=True)) as session:
        async with session.get(f'{url}/grid', auth=auth, ssl=False) as res:
            duties = []
            sem = asyncio.Semaphore(16)
            for community in networks:
                job = asyncio.ensure_future(load_network(sem, session, community))

            responses = asyncio.collect(*duties)
            await responses

Be aware: the decision to load_network(sem, session, community) – we implement that fairly merely with a easy operate the next:

async def load_network(sem, session, community):
    async with sem:
        await fetch(session, community)

The load_network methodology name takes a semaphore, our session, and a community object as args and asynchronously calls one other methodology known as fetch. That is the non-blocking methodology will make the async name to the server.

async def fetch(session, community):
    payload = dict(community=str(community), remark='test-network')
    async with session.publish(f'{url}/community', knowledge=payload, ssl=False) as res:
        if res.standing == 201:
            logger.data(f'efficiently inserted community {str(community)}')
            logger.error(f'did not load community {str(community)}')

The distinction is that it’s an async operate name. The operate is named with out ready for the response. See the total itemizing for wapi-async.py

If we graphically depict the stream and operation of our refactored async script for creating community objects, it could look one thing like the next:

Every numbered request is an async job which creates or inserts one IPv4 Community at a time. That is one more script which makes use of concurrency – the distinction is, the script is definitely single threaded. Asynchronous applications use cooperative multitasking as a substitute of preemptive multitasking. The working system doesn’t do any context switching, as a substitute, duties voluntarily yield management periodically – at completion, when they’re idle, or logically blocked. It’s mentioned to be cooperative as a result of all applications should cooperate to ensure that the scheduling scheme to work. Within the instance above, we use a Semaphore as a mechanism to regulate the max variety of duties that may run at any given time. That is analogous how we restricted the max variety of threads in our ThreadPool within the second article.


Once I began to check wapi-async.py in opposition to wapi-network.py, I observed efficiency of the script different broadly as I tuned the script for what number of semaphores to make use of. Subsequently, the script was examined quite a few instances with totally different semaphore values, beginning with 2 and ending with 32 semaphores. Discover how the efficiency drops off considerably once we use a price of 32!

Check Move #1 Move #2 Move #3
wapi-network.py 33.85 33.64 33.33
wapi-async.py w/ 2 semaphores 23.56 23.40 23.64
wapi-async.py w/ 4 semaphores 23.99 19.99 21.56
wapi-async.py w/ 8 semaphores 19.26 19.39 19.50
wapi-async.py w/ 16 semaphores 27.81 23.13 23.87
wapi-async.py w/ 32 semaphores 55.31 55.50 55.80
% enchancment 43.10% 42.36% 41.50%

The sweetspot appears to be 8 semaphores – outcomes could fluctuate based on how busy the Grid Grasp is. Recall, this testing is completed in an remoted lab with minimal load on the Grid Grasp. So, the sweetspot could also be decrease in a manufacturing atmosphere. The efficiency enchancment proven above is a results of evaluating our base script with the async script utilizing the sweetspot of 8.

NOTE: this take a look at was carried out with HTTP Keepalive enabled.

Total, the async script was greater than 40% sooner in loading the 1,024 networks into the Grid.


Plenty of totally different programming strategies have been mentioned on this sequence of articles to assist improve the efficiency of your automation scripts. Listed below are the principle takeaways:

  • Allow HTTP Keepalive in your Infoblox Grid – There’s no purpose to not!
  • Experiment with Python threading, and Python asyncio modules. See the place they slot in your serving to you develop your trove of scripts for bettering your automation.
  • Implement the Infoblox WAPI request module in your scripts when you’re performing predictable knowledge insert operations to load networks, zones, DNS useful resource data, and many others.
  • Experiment and Check (wash, rinse, repeat)

Whereas none of this stuff are going to be a panacea in all conditions, there may be going to be a time and alternative for every. In the event you study, experiment, and take a look at completely with these strategies to bolster your talent set, you’ll NOT be disillusioned and they’re going to serve you effectively!

Source link

Leave a Reply

Your email address will not be published.