Knowee
Questions
Features
Study Tools

Import wait_random from the previous python file that you’ve written and write an async routine called wait_n that takes in 2 int arguments (in this order): n and max_delay. You will spawn wait_random n times with the specified max_delay.wait_n should return the list of all the delays (float values). The list of the delays should be in ascending order without using sort() because of concurrency.

Question

Import wait_random from the previous python file that you’ve written and write an async routine called wait_n that takes in 2 int arguments (in this order): n and max_delay. You will spawn wait_random n times with the specified max_delay.wait_n should return the list of all the delays (float values). The list of the delays should be in ascending order without using sort() because of concurrency.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a step-by-step guide on how to implement the wait_n function in Python:

  1. First, you need to import the wait_random function from the previous file. Assuming the file is in the same directory and named previous_file.py, you can do this with the following line of code:
from previous_file import wait_random
  1. Next, you need to define the wait_n function. This function should be an asynchronous function, as it will be running multiple instances of wait_random concurrently. You can define an asynchronous function in Python using the async def keywords. The function should take two arguments: n and max_delay.
async def wait_n(n, max_delay):
  1. Inside the wait_n function, you need to create a list of tasks. Each task is an instance of the wait_random function with max_delay as the argument. You can create a task in Python using the asyncio.create_task function. Do this n times and store all tasks in a list.
tasks = [asyncio.create_task(wait_random(max_delay)) for _ in range(n)]
  1. Now, you need to run all tasks concurrently and store their results. You can do this using the asyncio.gather function, which returns a future aggregating results from the given tasks. Since you want the results to be in ascending order, you should use the asyncio.as_completed function instead, which returns an iterator that yields futures as they complete.
results = [task for task in asyncio.as_completed(tasks)]
  1. Finally, you need to return the results. Since asyncio.as_completed returns futures, you need to await each future in the results list to get the actual delay values.
return [await result for result in results]

Here is the complete code:

import asyncio
from previous_file import wait_random

async def wait_n(n, max_delay):
    tasks = [asyncio.create_task(wait_random(max_delay)) for _ in range(n)]
    return [await result for result in asyncio.as_completed(tasks)]

This function will run n instances of wait_random concurrently with max_delay as the argument, and return a list of the delay values in ascending order.

This problem has been solved

Similar Questions

import randomimport jsondef intermediate_step_multi_run_process(request):    def generate():        counter = 0        while counter < 2:            time.sleep(0.3)            random_no_list = []             for i in range(1000000000):                random_no = random.random() ** 2                random_no_list.append(random_no)            random_no_list = json.dumps(random_no_list)            yield "data:" + random_no_list + "\n\n"            counter += 1    return StreamingHttpResponse(generate(), content_type="text/event-stream")           The above stated is an async function triggered by a websocket in a web application to render the list of 100 million squared random numbers. What is the possible error that this piece of code could result in a wsgi application and why?

Given a queue with random elements, we need to sort it. We are not allowed to use extra space.Input Format: First Integer input (N), represents the size of the QueueNext N space separate integers indicate the values in the QueueOutput Format: Sorted list of elementsSample test cases :Input 1 :510 24 9 22 1Output 1 :1 9 10 22 24

On Python line 5, use random.randint() to generate a number between 0 and 2, and store the result to a variable named randomNumber.

Which of the following python function will return random floating point number between 0 and 1? random.float() random.randomfloat() random.frandom() random.random()

You are working as a software engineer for a bus ticketing system. The system needs to handle ticket requests efficiently. Each request arrives at the system with a timestamp representing the time when the request was made. To process these requests in the correct order, you need to sort them by their timestamps before handling them.Your task is to write a program that reads a list of ticket request timestamps, sorts them in ascending order, and then prints the sorted list. The queue of ticket requests will be implemented using a linked list.Input format :The first line consists of an integer n, the number of ticket requests.The second line consists of space-separated integers, representing the request timestamp.Output format :The output displays integers, representing the sorted request timestamps on a single line, separated by spaces.Refer to the sample output for formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:2 ≤ n ≤ 250 ≤ timestamp ≤ 100Sample test cases :Input 1 :419 29 12 18Output 1 :12 18 19 29 Input 2 :74 5 6 7 8 9 3 Output 2 :3 4 5 6 7 8 9 Input 3 :510 26 29 28 17Output 3 :10 17 26 28 29 Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.