用于电梯模拟的python线程

时间:2017-06-17 13:40:04

标签: python multithreading time

我正在尝试进行电梯模拟,因为我在CareerCup上看到了一个有趣的问题。我的问题是我希望电梯“花时间”从一层移动到另一层。现在它只是立即移动到“访问”列表的下一层。我不确定如何对它进行编程,以便在电梯移动时“拾取请求”可以进入。我认为这可能需要线程和time.sleep()函数。如何制作一个向电梯发出随机请求的线程,以及另一个让电梯试图满足所有请求的线程?这就是我到目前为止所做的:

import time
from random import *
import math

class Elevator:
    def __init__(self, num_floors):
        self.going_up = False
        self.going_down = False
        self.docked = True
        self.curr_floor = 0
        self.num_floors = num_floors
        self.floors_to_visit = []
        self.people_waiting = []

    def print_curr_status(self):
        for i in range(self.num_floors):
            if i == self.curr_floor:
                print('.  []')
            else:
                print('.')
        print ("to_visit: ", self.floors_to_visit)

    def handle_call_request(self, person):
        if not self.going_up and not self.going_down:
            self.floors_to_visit = [person.curr_floor] + self.floors_to_visit
            self.going_up = True
            self.docked = False
            self.people_waiting.append(person)
        else:
            self.floors_to_visit.append(person.curr_floor)
            self.people_waiting.append(person)

    def handle_input_request(self, floor_num):
        self.floors_to_visit.append(floor_num)

    def go_to_next(self):
        if not self.floors_to_visit:
            self.print_curr_status()
            return
        self.curr_floor = self.floors_to_visit.pop(0)
        for i,person in enumerate(self.people_waiting):
            if person.curr_floor == self.curr_floor:
                person.riding = True
                person.press_floor_num()
                self.people_waiting.pop(i)
        return


class Person:
    def __init__(self, assigned_elevator, curr_floor):
        self.curr_floor = curr_floor
        self.desired_floor = math.floor(random() * 10)
        self.assigned_elevator = assigned_elevator
        self.riding = False

    def print_floor(self):
        print(self.desired_floor)

    def call_elevator(self):
        self.assigned_elevator.handle_call_request(self)

    def press_floor_num(self):
        self.assigned_elevator.handle_input_request(self.desired_floor)


my_elevator = Elevator(20)

while True:
    for i in range(3):
        some_person = Person(my_elevator, math.floor(random() * 10))
        some_person.call_elevator()
    my_elevator.go_to_next()
    my_elevator.print_curr_status()
    time.sleep(1)

1 个答案:

答案 0 :(得分:0)

不需要进行任何线索。您可以引入2个新变量:一个跟踪电梯启动时间,另一个跟踪电梯应该采取的时间。然后只需检查电梯何时运行足够长时间。你可以这样做调用函数time.time();自1970年1月1日以来,它将以秒为单位返回时间(因为你只对它的差异感兴趣并不重要;你只需要一个随时间增加的函数)。虽然,这个功能往往不能给出比1秒更准确的时间段。如果您觉得机器上的信息不准确,那么您可以使用datetime

class Elevator:
    def __init__(self, num_floors):
        self.start_time = 0
        self.ride_duration = 1
        ...

    def call_elevator(self):
         self.start_time = time.time()
         self.assigned_elevator.handle_call_request(self)

    def go_to_next(self):
        if time.time() - self.start_time < self.ride_duration:
            return  # Do nothing.
        else:
            ...

您可能需要重构代码以满足您的需求,并在电梯使用时添加一些逻辑,等等。