python - 有什么好主意来衡量用户在队列中等待的时间吗?(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
解决方案
正如@deceze 评论的那样,只需在用户进入队列时保存时间戳。
考虑到如果您一到达就将用户推入队列,他们将按队列中的时间排序。在这种情况下,您可以使用 FIFO 队列来始终提取较早到达的用户。
但是,如果您想构建一个具有某种智能的配对系统,考虑到排队时间以外的其他因素(用户级别、地理区域......),请考虑到您不应该使用队列,而是使用更复杂的东西。
推荐阅读
- php - 带有标题的 SQL 查询表
- algorithm - 关于增强的第二次机会算法?
- c++ - C++ 调试控制台应用程序
- random - 函数 add_disk_randomness 是如何工作的?
- powershell - TFS 2015 构建步骤 Nuget Publisher“模糊选项's'”
- javascript - 节点js中数组对象的重叠
- oracle11g - Oracle DB 触发器失败,然后使用相同的输入成功
- python - 为什么 v*v 在 python 中比 v**2 快
- php - 使用 php move_uploaded_file 将文件上传到两级上层文件夹
- c++ - Box2D Contact Listener Collision 仅间歇性工作