Lab 1: Welcome to asyncio - Threads
Hands-On - Multithreading
Solution: Hands-On - Multithreading
import threading
# Task 1: Create a function `square_it()` which takes a single argument `n` and makes the function print (not return) the square of `n`.
def square_it(n):
print(n**2)
# Task 2: Create an another function `do_it()` which takes two integers a and b as arguments and creates two threads targeting `square_it()` function with a and b as argument respectively
def do_it(a, b):
t1 = threading.Thread(target=square_it, args=(a,))
t2 = threading.Thread(target=square_it, args=(b,))
# Task 3: Start the threads to view them in action.
t1.start()
t2.start()
Lab 2: Welcome to asyncio - Multiprocessing
Hands-On - Multiprocessing
Solution: fresco play Multiprocessing handson.
import multiprocessing
from multiprocessing import Pool
# Task 1: Create a function `calc_it()` that accepts two integers: `start' and `end`. It should then return the sum of all integers between `start' and `end' (including `start' and `end`) and return the resultant value.
def calc_it(start, end):
sum = 0
for i in range(start,end+1):
sum +=i
return sum
# Task 2: Create an another function `do_it()` that accepts a single integer: `n'. It should return the sum of all integers from 1 to `n' (including and `n`) using multiple processes and also effectively should spawn 4 processes targeting the `calc_it()` function.
# Hint: Use the `Pool` class and split the 1 to n range of the `do_it()` function into 4 sub ranges which are the passed to the sub-processes to be used as parameters to the `calc_it()` function.
def do_it(n):
num_processes = 4
# Calculate the range each process will handle
chunk_size = (n + num_processes - 1) // num_processes
ranges = [(i * chunk_size + 1, min((i + 1) * chunk_size, n)) for i in range(num_processes)]
with Pool(processes=num_processes) as pool:
results = pool.starmap(calc_it, ranges)
# Sum up the results from all processes
total_sum = sum(results)
print(total_sum)
return total_sum
Lab 3: Welcome to asyncio - Basic asyncio program
Hands-On - Hands-On - asyncio coroutines
Solution: fresco play Hands-On - asyncio coroutines handson.
import asyncio
#Let's try out a basic asyncio program. Make two coroutines (functions with `async`) named `question()` and `answer()'.
# Task 1: The question()` should display three questions. After each question, it should await on `answer()` to receive the answer. The answer is the same for all three questions.
# The questions that need to be displayed by `question()` are:
# "Is asyncio better for I0 bound programs than the CPU bound ones?"
# "Is async a keyword in the latest Python3?"
# "Is event loop the heart of an asyncio program?"
async def question():
#Complete this coroutine
print("Is asyncio better for IO bound programs than the CPU bound ones?")
# await asyncio.sleep(1)
await answer()
print("Is async a keyword in the latest Python3?")
await answer()
print("Is event loop the heart of an asyncio program?")
await answer()
# Task 2: The `answer()` should contain just a `print()` giving the message "Yes, it is!".
async def answer():
#Complete this coroutine
print("Yes, it is!")
asyncio.run(question())