Python

Python programming using Trio and Asks

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/

 

Manasa MB

Certified Python Developer and AWS geek. I am one of the people who is very passionate about programming and always love to keep myself updated with the latest tech.

Published by
Manasa MB

Recent Posts

Top 10 SEO Writing Tips for Beginners 2021

Search Engine Optimization. It’s complicated, to say the least. Search engines, like Google, are constantly…

4 days ago

Should you become a freelancer in 2021? Pros and Cons

Freelancing and working from home was long considered idyllic by many, but the global pandemic…

4 days ago

The Leading Renewable Energy Trends in 2021

The past year saw slowdowns in the ongoing shift toward renewable energy in many countries…

1 week ago

Top 20 Web Developer Portfolio templates

Are you in search for web developer portfolio templates? In this article, I have discussed…

4 weeks ago

The list of top websites to check the grammar of an essay

The hard work and dedication put into an essay should not be thrown away by…

1 month ago

Top Secure Coding Practices Based on OWASP Guidelines

When a good developer makes an app, they don’t leave behind any loose ends. They…

1 month ago