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.
Installing required python package using pip install
Now, we have trio and asks packages ready in the system. Let us code!
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
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)
trio.run()
by passing the function name and parameters to pass.
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)
asks.get()
is fetching movie data in parallel and updating to results_list variable.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
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/
IT is more important than ever in the world of higher education, and yet with…
If you’re here for the top tips, we assume you’re ahead of the “how to…
The world is progressing at unprecedented rates at the current moment, especially in terms of…
This article will highlight the Top 20 Opensource Python Tkinter Projects which we believe will…
With their numerous applications in streamlining the data flow, securing both the servers and the…
In this article, We will be looking at some of the top Node.js dashboard templates.…