Do you want to make many requests to lots of web pages in parallel and don’t want to write a lot of code to manage threads in python?

The answer is Trio and Asks packages for Asynchronous programming in python. 

Trio is one of the top 10 packages. It continues to be the star in concurrency programming in python.

Trio makes writing concurrent code simpler. It is relatively best and new when compared to other packages like Asyncio. As a result, we are using trio in our example.

Just remember two keywords async and await, you will write multithreaded code in python with ease.

Asks is similar to requests library in python. It supports,get()head() post(), put(), delete(),options() and request() functions.


We use get() function in our example below.

Asks package is to run multiple HTTP requests with Trio asynchronously.

Installation

Installing required python package using pip install

Now, we have trio and asks packages ready in the system. Let us code!

Use case: Movies playing in NewYork 

Let us see write code to pull data for movies playing in NewYork.

Using Gracenote web API to get theater and movie details.

We are using asks to make non-blocking HTTP requests to Gracenote web API  and trio to process many movies in parallel

Step1: Get a list of all movie theaters in NewYork using python code


import trio
import asks
BASE_URL = "http://data.tmsapi.com/v1.1/theatres"
API_KEY =  '<API KEY>'
async def get_theatres_nyc(zip_code):
    '''Get theatres data from Gracenote'''
    result = await asks.get(BASE_URL, params={'zip': str(zip_code),'api_key':API_KEY})
    return result.json()

asks.init(trio)
results_list = trio.run(get_theatres_nyc,10153)
print(results_list)
  • You must specify what event loop asks should use after importing asks, and at some point, before you run any code that uses asks.
  • Here asks is using trio so we have asks.init(trio)
  • The entry point is trio.run() by passing the function name and parameters to pass. 
  • Variable results_list contains complete theaters details which will be used in Step 2.
  • Results sample is shown below

Step2: Pull movies data for each theater in parallel using simple code.


import trio
import asks

BASE_URL = "http://data.tmsapi.com/v1.1/theatres"
API_KEY =  '<API KEY>'

async def get_movie_details(theatre):
    '''get movie and showtime data from Gracenote and update result'''
    theatre_id_num = theatre['theatreId']
    url = BASE_URL+'/'+str(theatre_id)+'/showings'
    result = await asks.get(url, params={'startDate': TODAY,'api_key':API_KEY})
    theatre['movies'] = result

async def movie_showtime_details(results_list):
    '''function to create nursery for each movie'''
    async with trio.open_nursery() as nursery:
    for theatre in results_list:
        if theatre.get('theatreId'):
            nursery.start_soon(get_movie_details, theatre)
    return results_list

asks.init(trio)
#results_list is from step 1 code
results_list = trio.run(movie_showtime_details, results_list)
print(results_list)

 

  • Open a trio nursery, we are starting a trio task for each movie.
  • asks.get() is fetching movie data in parallel and updating to results_list variable.
  • Await keyword is used to call the function concurrently.
  • Most importantly when await is used, trio will take care of thread wait and send back the results.

Get theaters, movies and showtime details in the rate of 20 requests/second. Find the complete source code in the GitHub repository:

View on GithHub

 

Links to refer 

GitHub: https://github.com/python-trio/trio, https://github.com/theelous3/asks

Docs: https://trio.readthedocs.io, https://asks.readthedocs.io/en/latest/

PyPi: https://pypi.org/project/trio/, https://pypi.org/project/asks/