0% found this document useful (0 votes)
2 views3 pages

Assignment 1 Asyncoi

The assignment focuses on exploring asynchronous programming in Python using the asyncio library by creating a simulated online bookstore. Students will implement coroutines to fetch book details and process orders asynchronously, while managing the event loop effectively. The final deliverable is a Python script that demonstrates these concepts and is due before the start of week 4 lecture.

Uploaded by

sukhmansinghspsk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Assignment 1 Asyncoi

The assignment focuses on exploring asynchronous programming in Python using the asyncio library by creating a simulated online bookstore. Students will implement coroutines to fetch book details and process orders asynchronously, while managing the event loop effectively. The final deliverable is a Python script that demonstrates these concepts and is due before the start of week 4 lecture.

Uploaded by

sukhmansinghspsk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment #1: Exploring Asynchronous Programming with asyncio in

Python
Objective:
The goal of this assignment is to deepen your understanding of asynchronous
programming in Python using the asyncio library. You will implement various
asynchronous tasks, utilize coroutines, manage the event loop, and demonstrate
your ability to handle I/O-bound operations efficiently.
Assignment Overview:
You will create a Python program that simulates an online bookstore. The program
will:
1. Fetch book details from a list of URLs (simulated API calls).
2. Perform asynchronous operations to simulate order processing.
3. Use the concepts of coroutines, tasks, and the event loop effectively.
Tasks:
1. Set Up Your Environment:
o Ensure you have Python 3.7 or later installed.

o Create a new Python file named async_bookstore.py.

2. Simulate Fetching Book Data:


o Define a list of simulated URLs for fetching book details (you can use
placeholder URLs). Eg. Book_urls = [“https://api.example.com/book1”,
“https://api.example.com/book2”, “https://api.example.com/book3”]
o Create a coroutine named fetch_book_details(url) that simulates
fetching book details asynchronously. You can use asyncio.sleep() to
mimic network latency.
o The coroutine should return a dictionary containing book information
(e.g., title, author, price).
Example:
async def fetch_book_details(url):
await asyncio.sleep(1) # Simulate network delay
return {"title": "Sample Book", "author": "Author Name", "price": 19.99}
3. Order Processing Simulation:
o Create a coroutine named process_order(book_details) that simulates
processing a book order. This coroutine should also use asyncio.sleep()
to mimic a delay in order processing.
o The function should return a confirmation message once the order is
processed.
4. Main Coroutine:
o Create an async def main() coroutine that:

 Uses asyncio.gather() to fetch book details for all URLs


concurrently using a variable length argument. (Hint: only use
asyncio.gather() once to fetch all of the books details.)
 Processes each book order by calling process_order() for each
fetched book detail using a variable length argument. (Hint:
only use asyncio.gather() once to confirm the orders.)
 Prints the book details and the order confirmation messages.
Example of output:
Fetched: Sample Book from https://api.example.com/book1
by Author Name - Price: $22.99
Order processed for 'Sample Book from
https://api.example.com/book1' by Author Name
Fetched: Sample Book from https://api.example.com/book2
by Author Name - Price: $24.99
Order processed for 'Sample Book from
https://api.example.com/book2' by Author Name
Fetched: Sample Book from https://api.example.com/book3
by Author Name - Price: $28.99
Order processed for 'Sample Book from
https://api.example.com/book3' by Author Name
5. Run the Program:
o Use asyncio.run(main()) to execute your main coroutine and manage
the event loop.
6. Documentation:
o Comment your code to explain the functionality of each part.

o Include a brief description at the top of your script explaining the


purpose of the program.
Deliverables:
 Submit your async_bookstore.py file via Moodle as a group, ie one submission
per group.
 Ensure your code runs without errors and produces the expected output.
Evaluation Criteria:
 Correctness: The program runs without errors, and all asynchronous
operations function as intended and use of variable length arguments.
 Code Organization: Code is well-structured, with appropriate use of
functions and comments.
 Use of Asyncio: Effective use of coroutines, tasks, and the event loop.
 Documentation: Clear comments and a description of the program's
purpose.
Deadline:
 Please submit your assignment before start of week 4 lecture.
Additional Resources:
 Python asyncio Documentation
This assignment will help you apply the concepts of asynchronous programming in a
practical scenario, reinforcing your understanding of asyncio and its applications in
real-world programming tasks.

You might also like