mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-29 05:18:10 +00:00
Lock dispatcher groups. Fixes #255
This commit is contained in:
parent
0699bd31e5
commit
8d0e161b56
@ -20,7 +20,7 @@ import logging
|
|||||||
import threading
|
import threading
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from queue import Queue
|
from queue import Queue
|
||||||
from threading import Thread
|
from threading import Thread, Lock
|
||||||
|
|
||||||
import pyrogram
|
import pyrogram
|
||||||
from pyrogram.api import types
|
from pyrogram.api import types
|
||||||
@ -64,6 +64,8 @@ class Dispatcher:
|
|||||||
self.updates_queue = Queue()
|
self.updates_queue = Queue()
|
||||||
self.groups = OrderedDict()
|
self.groups = OrderedDict()
|
||||||
|
|
||||||
|
self.lock = Lock()
|
||||||
|
|
||||||
self.update_parsers = {
|
self.update_parsers = {
|
||||||
Dispatcher.MESSAGE_UPDATES:
|
Dispatcher.MESSAGE_UPDATES:
|
||||||
lambda upd, usr, cht: (pyrogram.Message._parse(self.client, upd.message, usr, cht), MessageHandler),
|
lambda upd, usr, cht: (pyrogram.Message._parse(self.client, upd.message, usr, cht), MessageHandler),
|
||||||
@ -110,6 +112,7 @@ class Dispatcher:
|
|||||||
self.groups.clear()
|
self.groups.clear()
|
||||||
|
|
||||||
def add_handler(self, handler, group: int):
|
def add_handler(self, handler, group: int):
|
||||||
|
with self.lock:
|
||||||
if group not in self.groups:
|
if group not in self.groups:
|
||||||
self.groups[group] = []
|
self.groups[group] = []
|
||||||
self.groups = OrderedDict(sorted(self.groups.items()))
|
self.groups = OrderedDict(sorted(self.groups.items()))
|
||||||
@ -117,6 +120,7 @@ class Dispatcher:
|
|||||||
self.groups[group].append(handler)
|
self.groups[group].append(handler)
|
||||||
|
|
||||||
def remove_handler(self, handler, group: int):
|
def remove_handler(self, handler, group: int):
|
||||||
|
with self.lock:
|
||||||
if group not in self.groups:
|
if group not in self.groups:
|
||||||
raise ValueError("Group {} does not exist. Handler was not removed.".format(group))
|
raise ValueError("Group {} does not exist. Handler was not removed.".format(group))
|
||||||
|
|
||||||
@ -142,6 +146,7 @@ class Dispatcher:
|
|||||||
else (None, type(None))
|
else (None, type(None))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
with self.lock:
|
||||||
for group in self.groups.values():
|
for group in self.groups.values():
|
||||||
for handler in group:
|
for handler in group:
|
||||||
args = None
|
args = None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user