Question: PLEASE SOLVE IN PYTHON. JUST CODE Task 4 . 2 . Define two coroutines, one of which will be used to retrieve facts about cats

PLEASE SOLVE IN PYTHON. JUST CODE Task 4.2. Define two coroutines, one of which will be used to retrieve facts about cats using the get_cat_fact coroutine that sends a GET request. Create 20 Task objects to retrieve facts about cats and call them within the main coroutine and store the results at once using the asyncio.gather function. The second coroutine filter_cat_facts does not send requests, but must receive a ready-made list of facts about cats and return a new list containing only those facts that contain the word "cat" or "cats".
Task 5.3. Define a coroutine get_dog_fact that retrieves facts about dogs.
Let the coroutine get_dog_fact retrieve the fact about dogs. After that, define a coroutine get_cat_fact that retrieves the fact about cats by sending a request.
At the same time, store the results of executing these Tasks using the asyncio.gather(* dog_facts_tasks, *cat_facts_tasks) function in the list dog_cat_facts, and then use list slicing to separate them into two lists
since you know that the first 5 facts are about dogs and the second 5 are about cats.
Finally, define a third coroutine mix_facts that takes the lists dog_facts and cat_facts and returns a new list that contains the dog fact for the index value if the length of the dog fact is greater than the length of the cat fact at the same index, otherwise it returns the cat fact. Finally, print the results of the filtered fact array. You can iterate over the lists in parallel using the zip function, e.g. for dog_fact, cat_fact in zip(dog_facts, cat_facts).
Task 6.
Define a fetch_users coroutine that will send a GET request to the JSONPlaceholder.
You need to simulate sending 5 requests concurrently
within the main coroutine. Inside the main coroutine, measure the execution time of the program, and store the results in a list at once using the asyncio.gather function. Then, using the map function or
list comprehension, extract them into 3 separate lists: only user names, only user email addresses, and only user usernames. At the end of the main coroutine, print all 3 lists and the execution time of the program.
PLEASE SOLVE IN PYTHON. JUST CODE Task 4 . 2 .

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!