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

How to Create the Perfect Branded Website for Your Business

Marketing has now fully gone online so that the first thing you think of as…

2 days ago

Top 20 Tailwind CSS Dashboard Templates and themes

Tailwind CSS framework allows high customization and provides you with construction blocks for you to…

3 days ago

Top 19 Opensource Form Builder projects

Form Builder projects enables you to develop job-specific and customizable forms. They collect responses from…

1 week ago

Top 20 OpenSource Neural networks projects

Are you looking for an open source neural network project? In this article you will…

1 week ago

5 Tools for Effective Academic Writing

Not every student is able to comprehend their assignment. Some struggle with the basic task…

2 weeks ago

PowerPoint Tips for Software Professional

Knowing how to give a successful presentation will assist you in landing a job, making…

2 weeks ago