Use a queue to treat incoming messages instead of using server thread to answer

This commit is contained in:
Némunaire 2012-08-23 18:20:45 +02:00
parent c92160f041
commit fce491552b
5 changed files with 77 additions and 17 deletions

26
bot.py
View file

@ -17,27 +17,33 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from datetime import datetime
from queue import Queue
import threading
from consumer import Consumer
import event
import hooks
from server import Server
class Bot:
def __init__(self, servers=dict(), modules=dict(), mp=list()):
self.version = 3.2
self.version = 3.2
self.version_txt = "3.2"
self.servers = servers
self.modules = modules
self.modules_path = mp
self.datas_path = './datas/'
self.datas_path = './datas/'
self.hooks = hooks.MessagesHook()
self.events = list()
self.hooks = hooks.MessagesHook()
self.events = list()
self.event_timer = None
self.msg_queue = Queue()
self.msg_thrd = list()
self.msg_thrd_size = -1
def add_event(self, evt):
"""Register an event"""
@ -131,6 +137,18 @@ class Bot:
return False
def receive_message(self, srv, raw_msg, private = False):
"""Queued the message for treatment"""
self.msg_queue.put_nowait((srv, raw_msg, private))
# Launch a new thread if necessary
if self.msg_queue.qsize() > self.msg_thrd_size:
c = Consumer(self)
self.msg_thrd.append(c)
c.start()
self.msg_thrd_size += 2
def quit(self, verb=False):
"""Save and unload modules and disconnect servers"""
if self.event_timer is not None: