【问题标题】:python threading for elevator simulation用于电梯模拟的python线程
【发布时间】:2017-11-20 04:16:32
【问题描述】:

由于我在 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)

【问题讨论】:

    标签: python multithreading time


    【解决方案1】:

    没有必要进行线程化。您可以引入 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:
                ...
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-31
      • 2021-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多