Search code examples
pythontimemeasure

Is there any good idea to measure a time of user waited in queue? (Python)


I'm making a matchmaking system using Python, and I need to measure the times user waited in a queue.

Is there any better idea to optimize counting time?

I wrote a code like this using Thread. This is a part of my code.

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

Solution

  • As @deceze commented, just save the timestamp when user enter into the queue.

    Take into account that if you push users in the queue as soon as they arrive, they will be sorted by time in the queue. In this case, you can use a FIFO queue to extract always de user who arrives earlier.

    However, if you want to build a matchmaking system with some intelligence taking into account something else than time on queue (level of the user, geografical zone...), take into account you shouldn't use a queue but something more complex.