Categories: PythonTech

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

3 Ways to Get the Most Out of Your University’s Virtual Computer Lab

IT is more important than ever in the world of higher education, and yet with…

21 hours ago

Top Tips for Learning Java Programming

If you’re here for the top tips, we assume you’re ahead of the “how to…

1 day ago

Neural Networks for Creating Blog Texts

The world is progressing at unprecedented rates at the current moment, especially in terms of…

2 days ago

Top 20 Opensource Python Tkinter Projects

This article will highlight the Top 20 Opensource Python Tkinter Projects which we believe will…

4 days ago

Beginners guide to Sneaker Proxies

With their numerous applications in streamlining the data flow, securing both the servers and the…

1 week ago

Top 20 Node.js dashboard templates

In this article, We will be looking at some of the top Node.js dashboard templates.…

1 week ago