def run(self):

        if DEBUG: dbg("[DBG] HB INIT")

        while(True):
            if self.stopped():
                return
            try:
                # Sleep at most HEARTBEAT_MAX_SLEEP. We will most likely overwrite this
                # with a lower value, depending on the pending requests.
                time_to_sleep = HEARTBEAT_MAX_SLEEP

                # Loop through every user that is using the experiment concurrently, and consider
                # whether we should update it or not.
                if DEBUG_HEARTBEAT_MESSAGES: print "[DBG] HB Listing sessions..."

                for session_id in self.session_manager.list_sessions():
                    if DEBUG_HEARTBEAT_MESSAGES: print "[DBG] HB checking session_id=%s" % (session_id)
                    try:
                        sessiondata = self.session_manager.get_session_locking(session_id)
                    except SessionNotFoundError:
                        if DEBUG_HEARTBEAT_MESSAGES: print "[DBG] HB no session found for session_id=%s" % (session_id)
                        continue


                    if 'sessionkey' not in sessiondata:
                        self.session_manager.modify_session_unlocking(session_id, sessiondata)
                        if DEBUG_HEARTBEAT_MESSAGES: print "[DBG] HB no sessionkey found in sessiondata for session_id=%s" % (session_id)
                        continue

                    try:

                        # Evaluate the time left for the next potential heartbeat.
                        if 'last_heartbeat_sent' not in sessiondata:
                            # If we actually don't have a last_heartbeat_sent yet,
                            # initialize it.
                            sessiondata['last_heartbeat_sent'] = time.time()
                        last_sent = sessiondata['last_heartbeat_sent']
                        session_key = sessiondata['sessionkey']
                    finally:
                        self.session_manager.modify_session_unlocking(session_id, sessiondata)

                    time_left = (last_sent + self.heartbeat_period) - time.time()

                    # If time_left is zero or negative, a heartbeat IS due.
                    if(time_left <= 0):
                        if DEBUG_HEARTBEAT_MESSAGES: dbg("[DBG] HB FORWARDING")
                        ret = self.experiment.forward_request(session_id, HEARTBEAT_REQUEST % (session_key))
                        if DEBUG_HEARTBEAT_MESSAGES: dbg("[DBG] Heartbeat response: %s" % ret)

                    else:
                        # Otherwise, we will just sleep.
                        if DEBUG_HEARTBEAT_MESSAGES: print "[DBG] HB SLEEPING FOR %d" % (time_left)

                        # We wish to sleep the maximum only if there are no pending
                        # requests (and if it doesn't go over the MAX).
                        time_to_sleep = min(time_left, time_to_sleep)

                if DEBUG_HEARTBEAT_MESSAGES: print "[DBG] Listing sessions finished"
                sys.stdout.flush()

                # We will actually not sleep the whole time_to_sleep at once, so that
                # we can check whether the thread has finished more often.
                # TODO: Consider doing this through semaphores so that we do not need
                # this work-around.
                step_time = 0.1
                steps = time_to_sleep / step_time
                while not self.stopped() and steps > 0:
                    time.sleep(step_time)
                    steps -= 1
                if DEBUG_HEARTBEAT_MESSAGES: print "[DBG] Not sleeping anymore"
                sys.stdout.flush()

            except:
                # TODO: use log
                traceback.print_exc()
                time.sleep(5)
run()
dataset/ETHPy150Open weblabdeusto/weblabdeusto/server/src/experiments/visir_html5.py/Heartbeater.run