首页 > 解决方案 > 有什么好主意来衡量用户在队列中等待的时间吗?(Python)

问题描述

我正在使用 Python 制作一个配对系统,我需要测量用户在队列中等待的时间。

有没有更好的方法来优化计数时间?

我使用 Thread 编写了这样的代码。这是我的代码的一部分。

from threading import Thread
from time import sleep

class Matchmaking:
    def __init__(self):
        self._queue = Queue()
        self.timer = Thread(target=self._update_pending_time)
        self.timer.start()
        self.timer.join

    def _update_pending_time(self):
        sleep(1)
        # get_user_in_queue() returns DataFrame
        if not self._queue.get_user_in_queue().empty:
            self.waited_times = self._queue.get_user_in_queue().loc[:, "pending_time"] + 1

标签: pythontimemeasure

解决方案


正如@deceze 评论的那样,只需在用户进入队列时保存时间戳。

考虑到如果您一到达就将用户推入队列,他们将按队列中的时间排序。在这种情况下,您可以使用 FIFO 队列来始终提取较早到达的用户。

但是,如果您想构建一个具有某种智能的配对系统,考虑到排队时间以外的其他因素(用户级别、地理区域......),请考虑到您不应该使用队列,而是使用更复杂的东西。


推荐阅读