Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of queue instance of Python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this "Python queue instance Analysis" article, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Python queue instance Analysis" article.

Simulate the printer task queue process

There are also many examples of queues in computer science. For example, the computer laboratory has 10 computers, all of which are connected to the same printer. When students need to print, their printing tasks enter a queue. The first task in the queue is the print task that is about to be performed. If a task is at the end of the queue, it must wait until all previous tasks have been executed before it can be executed.

Students send print requests to the shared printer, and these print tasks are stored in a queue and executed on a first-come-first-served basis. Such a setting can cause a lot of problems. The most important thing is whether the printer can handle a certain amount of work. If not, students may miss the class because they wait too long.

Consider a scenario in a computer lab where there are 10 students in any given hour. They print up to two times in an hour, and the number of printed pages ranges from 1 to 20 pages. The printer in the laboratory is so old that it can only print 10 pages of low quality per minute. You can also increase the print quality, but doing so will cause the printer to print only 5 pages per minute. Slowing down the printing speed may cause students to wait too long. So, how should you set the print speed?

You can solve this problem by building a model. We need to build objects for students, printing tasks, and printing institutions. When students submit print tasks, we need to add them to the printer's task queue. When the printer finishes performing a task, it checks the queue to see if there are any tasks that need to be processed. What we are interested in is how long the average student has to wait to get the printed article. This time is equal to the average waiting time of the print task in the queue.

In the simulation, we need to apply some knowledge of probability. For example, a student may print an article of 1 to 20 pages. If the probability of the number of pages is equal, the actual length of the print task can be calculated by simulating a random number of pages from 1 to 20.

If there are 10 students in the lab and everyone prints twice in an hour, there will be an average of 20 printing tasks per hour. What is the probability of creating a print request at any second? To answer this question, you need to consider the ratio of task to time. 20 tasks per hour is equivalent to 1 task every 180 seconds.

The probability of producing a print request per second (the probability of 1 stroke 180) can be simulated by a random number of 1: 180. If the random number happens to be 180, then a print request is considered to have been created. Note that multiple requests may be created one after another, or there may be no requests for a long time. This is the essence of simulation. We want to simulate as accurately as possible when the common parameters are known.

Main simulation steps:

1. Create a queue of print tasks. Every task will have a time stamp when it comes. At first, the queue was empty.

two。 Do the following for each second (currentSecond).

Is there a newly created print task? If so, use currentSecond as its timestamp and add the task to the queue.

If the printer is idle and there are tasks waiting to be performed, do the following:

Take the first task out of the queue and submit it to the printer

Use currentSecond to subtract the timestamp of the task to calculate its waiting time

Store the wait time of the task in a list, which is used as data to calculate the average wait time

Calculate the execution time based on the number of pages of the task.

The printer prints for one second and subtracts one second from the execution time of the task.

If the execution of the print task is complete, that is, the execution time of the task is reduced to 0, the printer is back idle.

3. When the simulation is complete, the average wait time is calculated based on the values in the wait time list.

Build queue program class Queue: def _ init__ (self): self.items = [] # build empty queue def isEmpty (self): return self.items = = [] # determine whether it is empty def enqueue (self,item): self.items.insert (0 Item) # insert element def dequeue (self) at the end of the queue (left end of the list): return self.items.pop () # remove the element def size (self): return len (self.items) # queue length Simulation Printer class Printer: # Printer initialization def _ _ init__ (self) in the queue header (right end of the list) Ppm): self.pagerate = ppm # print speed page / minute self.currentTask = None # existing task self.timeRemain = 0 # time required for this task # print task countdown 0 represents print completion def tick (self): # if the printer is performing a task if self.currentTask! = None: # the task execution time = execution time-1 (execution time countdown) self.timeRemaining = self.timeRemaining-1 if self.timeRemaining

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report