当前位置: 首页>>代码示例>>Python>>正文


Python Queue.update_head方法代码示例

本文整理汇总了Python中queue.Queue.update_head方法的典型用法代码示例。如果您正苦于以下问题:Python Queue.update_head方法的具体用法?Python Queue.update_head怎么用?Python Queue.update_head使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在queue.Queue的用法示例。


在下文中一共展示了Queue.update_head方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: simulate_queue

# 需要导入模块: from queue import Queue [as 别名]
# 或者: from queue.Queue import update_head [as 别名]
def simulate_queue(mar, ast):
    """
    Function to iterate through events and perform the main queue simulation.
    
    Parameters: mean arrival time and average service time.
    Returns: mean queue length and mean waiting time per customer.
    """

    # Initialize the queue
    q = Queue()

    # This variable stores the arrival time of the last event to enter the
    # queue. This is required because only the inter-arrival times of events
    # are known, not their absolute arrival times.
    last_arrival_time = 0.0

    # Global time parameter. This will usually point to the exit time of the
    # item that is currently at the head of the queue.
    t = 0.0

    # Variables to record waiting times and queue length
    num_events = 0
    total_waiting_time = 0.0
    total_length_time = 0.0

    # Start iterating through events
    for event in events(mar, ast):
        print t
        # Figure out the new event's absolute arrival time...
        event.arrival_time = last_arrival_time + event.inter_arrival_time
        # ...and update last_arrival_time to match this event
        last_arrival_time = event.arrival_time
        
        # We'll need to compare the arrival time of the new event with the exit
        # time of the event at the head of the queue
        head = q.head()
        if head is None:
            # If the queue is empty, just add the new event.
            event.exit_time = event.arrival_time + event.service_time
            q.enqueue(event)
            # No one else can get serviced while this event is at the counter.
            # Let's skip to when this event exits.
            t = event.exit_time
            continue
        if head.exit_time > event.arrival_time:
            # We're now at the head's exit time, but this new event came in
            # before the head could finish. So the new event gets queued.
            q.enqueue(event)
            # This event must now wait in the queue until the head exits. This
            # means that it adds one to the queue length until then. We'll take
            # care of queue lengths after then later.
            total_length_time += 1 * (head.exit_time - event.arrival_time)
            # We can't do anything more. Let's check if another event happened
            # to come in before the head could finish.
            continue
        if head.exit_time <= event.arrival_time:
            # This event actually arrived just when or after the head finished.
            # Let's queue the new event for now.
            q.enqueue(event)
            # The head is done, so let's remove it.
            completed_event = q.dequeue()
            # We need to catch the head and ask it how long it waited in the
            # queue
            total_waiting_time += (  completed_event.exit_time
                                   - completed_event.arrival_time
                                   - completed_event.service_time )
            num_events += 1
            # Before we enqueue the new event, we need to check whether 
            # We now need to set the new head's exit time - note that the
            # presence of a new head is guaranteed, since we just added a new
            # event.
            # If the head is the newly added event, then we need to consider
            # time it takes since *its* arrival. Otherwise, we consider the
            # time taken since the previous event exited.
            l = len(q)
            if l == 1:
                new_exit_time = event.arrival_time + event.service_time
            else:
                new_exit_time = t + q.head().service_time
            q.update_head('exit_time', new_exit_time)
            # In order to update the total_length_time, we note that the events
            # still in the queue need to wait until the new head completes. So,
            # each waiting queue member adds one queue length until then.
            # Note: l must be at least 1, since a new event was added.
            if l == 1:
                # l is 1, meaning that the new event immediately became the
                # head. Hence, it does not contribute to the queue length.
                pass
            else:
                # l is greater than or equal to 2. Now, there is a new event
                # which contributes one queue length, starting from its time
                # of arrival, until the new head's exit time.
                total_length_time += 1 * (new_exit_time - event.arrival_time)
                # Additionally, there are potentially another (l-2) events that
                # were already in the queue. These add one queue length *each*
                # until the new head's exit time.
                total_length_time += (l - 2) * (new_exit_time - t)
            # Once again, no one can get serviced until the new head leaves. So
            # it's safe to jump forward in time.
            t = new_exit_time
#.........这里部分代码省略.........
开发者ID:praveenv253,项目名称:idsa,代码行数:103,代码来源:queue-averages.py


注:本文中的queue.Queue.update_head方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。