Fix message timing on empty queue

This commit is contained in:
Gergo Turcsanyi 2020-05-29 12:01:37 +02:00
parent e58f0b1d04
commit e5d0e91e93

View File

@ -27,14 +27,15 @@ class MessageQueueHandler:
def _dispatch_messages(self): def _dispatch_messages(self):
for message in iter(self._queue.get, None): for message in iter(self._queue.get, None):
message['typing'] = not self._queue.empty() is_queue_empty = self._queue.empty()
message['typing'] = not is_queue_empty
self.connector.send_message(message) self.connector.send_message(message)
self._sleep(self._get_sleep_time(message)) self._sleep(self._get_sleep_time(message, is_queue_empty))
def _get_sleep_time(self, message): def _get_sleep_time(self, message, is_queue_empty):
words_per_min = message['wpm'] if 'wpm' in message else self.wpm words_per_min = message['wpm'] if 'wpm' in message else self.wpm
chars_per_min = self.avg_word_len * words_per_min / 60 chars_per_min = self.avg_word_len * words_per_min / 60
return len(message['message']) / chars_per_min return 0 if is_queue_empty else len(message['message']) / chars_per_min
def _sleep(self, seconds): def _sleep(self, seconds):
poll_freq = self.drain_poll_freq poll_freq = self.drain_poll_freq