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 Define two coroutines, one of which will be used to retrieve facts about cats using the getcatfact coroutine that sends a GET request. Create 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 filtercatfacts does not send requests, but must receive a readymade list of facts about cats and return a new list containing only those facts that contain the word "cat" or "cats".
Task Define a coroutine getdogfact that retrieves facts about dogs.
Let the coroutine getdogfact retrieve the fact about dogs. After that, define a coroutine getcatfact 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 dogfactstasks, catfactstasks function in the list dogcatfacts, and then use list slicing to separate them into two lists
since you know that the first facts are about dogs and the second are about cats.
Finally, define a third coroutine mixfacts that takes the lists dogfacts and catfacts 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, eg for dogfact, catfact in zipdogfacts, catfacts
Task
Define a fetchusers coroutine that will send a GET request to the JSONPlaceholder.
You need to simulate sending 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 separate lists: only user names, only user email addresses, and only user usernames. At the end of the main coroutine, print all lists and the execution time of the program.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
