PYTHON Asynchronous libraries and threading in Python
Learning Outcomes:
How to use async features and threading for handling tasks with high latency (long running / time delay) for long-running processes.
Here's an example of a Python laboratory activity designed to help understand asynchronous programming and threading in Python.
This lab activity will walk through examples using Python's asyncio library and the threading module, showing how each can be used to handle tasks with high latency or long-running processes.
Let’s do this in Google Collab: works with Gmail Login Credentials
responses = await asyncio.gather(*(fetch_url(url) for url in urls))
print(responses[0][:100]) # Print first 100 characters of the response
asyncio.run(main())
Comments:
aiohttp.ClientSession() provides a session for making HTTP requests.
asyncio.gather() is used again to handle multiple requests simultaneously.
Here's an adaptation of the program from the asynchronous programming section that fetches URLs and writes the contents retrieved from each website to its own file in the current default directory.
This version uses the aiohttp library for making HTTP requests asynchronously and incorporates file writing operations:
# Start tasks to fetch URLs and write contents to files
await asyncio.gather(*(fetch_and_save(url) for url in urls))
# Run the main function to start the async program
asyncio.run(main())
Explanation of Changes:
Function fetch_and_save: This function handles both fetching the website content and writing it to a file.
It generates a filename by stripping the protocol from the URL (http or https) and replacing slashes with underscores to avoid directory traversal issues.
It then writes the content it retrieves to this file.
File Writing: The open() function is used to create and write to the file in 'write' mode ('w'). This ensures that any existing file with the same name is overwritten. The filename is created dynamically from the URL to ensure uniqueness for each site's content.
Error Handling: Basic error handling such as checking the response status or handling exceptions during the HTTP request or file operations is assumed to be part of the production-level code and should be added based on specific requirements.
This script demonstrates how to combine asynchronous HTTP requests with file I/O in Python, useful for web scraping tasks, data collection, or caching web content locally.
Part 2: Using Threading for Long-Running Tasks
Step 1: Basic Threading Example
import threading
import time
def print_numbers():
for i in range(1, 6):
time.sleep(1)
print(i)
# Create a thread that runs the print_numbers function
thread = threading.Thread(target=print_numbers)
thread.start()
# Continue with the main thread
print("Thread started")
thread.join() # Wait for the thread to complete
print("Thread has completed")
Comments:
threading.Thread() is used to create a new thread.
.join() makes the main thread waituntil the created thread completes.
Utilizing threads to perform I/O operations allows the main program to continue running without waiting for the I/O operations to complete, unless explicitly joined.
Conclusion:
This lab provides a practical understanding of how asynchronous programming with asyncio and threading can be used to efficiently handle tasks that involve high latency operations or long-running processes.
Experiment by modifying the tasks or combining threading with asyncio to observe different behaviors and performances.
Want to print your doc? This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (