溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python編程實現(xiàn)打印任務(wù)監(jiān)控

發(fā)布時間:2024-08-05 13:58:06 來源:億速云 閱讀:85 作者:小樊 欄目:編程語言
class PrintQueue:
    def __init__(self):
        self.items = []

    def is_empty(self):
        return len(self.items) == 0

    def enqueue(self, item):
        self.items.insert(0, item)

    def dequeue(self):
        return self.items.pop()

    def size(self):
        return len(self.items)


class Printer:
    def __init__(self, ppm):
        self.ppm = ppm
        self.current_task = None
        self.time_remaining = 0

    def tick(self):
        if self.current_task is not None:
            self.time_remaining -= 1
            if self.time_remaining <= 0:
                self.current_task = None

    def busy(self):
        return self.current_task is not None

    def start_next(self, new_task):
        self.current_task = new_task
        self.time_remaining = new_task.get_pages() * 60 / self.ppm


class Task:
    def __init__(self, time):
        self.time = time

    def get_pages(self):
        return self.time


def simulation(num_seconds, pages_per_minute):
    printer = Printer(pages_per_minute)
    print_queue = PrintQueue()
    waiting_times = []

    for current_second in range(num_seconds):
        if new_print_task():
            task = Task(random.randint(1, 20))
            print_queue.enqueue(task)

        if not printer.busy() and not print_queue.is_empty():
            next_task = print_queue.dequeue()
            waiting_times.append(next_task.time)
            printer.start_next(next_task)

        printer.tick()

    average_wait = sum(waiting_times) / len(waiting_times)
    print("Average Wait Time: {:.2f} seconds, {} tasks remaining.".format(average_wait, print_queue.size()))


def new_print_task():
    num = random.randint(1, 180)
    return num == 180


if __name__ == '__main__':
    import random
    for i in range(10):
        simulation(3600, 5)  # Simulate for 1 hour at 5 pages per minute

這段代碼實現(xiàn)了一個簡單的打印任務(wù)監(jiān)控系統(tǒng)。它模擬了一個打印隊列和打印機,根據(jù)隨機生成的打印任務(wù)來模擬打印過程。在主程序中,我們可以調(diào)用simulation函數(shù)來運行模擬,輸出平均等待時間和剩余任務(wù)數(shù)。在這里,我們模擬了10次1小時內(nèi)以每分鐘5頁的速度打印的情況。您可以根據(jù)實際需求調(diào)整模擬的時間和打印速度。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI