Make dispatch multithreading safe
Guard the execution of dispatch with a recursive thread lock. This is needed to make a thread safe way to send events to Client objects. Note that the only thread safe method is dispatch, everything else is unsafe to call from another thread, as the thead handling the Client object could be modifying arbitrary structures at any time. In addition this only keeps nasal demons away, and does not solve any of the difficult syncronization issues that might be present.
This commit is contained in:
parent
5e671a0d0d
commit
247d1f9ed4
@ -353,6 +353,7 @@ class Client(object):
|
|||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
self._is_logged_in = False
|
self._is_logged_in = False
|
||||||
self.connection = ConnectionState(self.dispatch, **kwargs)
|
self.connection = ConnectionState(self.dispatch, **kwargs)
|
||||||
|
self.dispatch_lock = threading.RLock()
|
||||||
self.token = ''
|
self.token = ''
|
||||||
self.events = {
|
self.events = {
|
||||||
'on_ready': _null_event,
|
'on_ready': _null_event,
|
||||||
@ -435,6 +436,7 @@ class Client(object):
|
|||||||
object.__setattr__(self, name, value)
|
object.__setattr__(self, name, value)
|
||||||
|
|
||||||
def dispatch(self, event, *args, **kwargs):
|
def dispatch(self, event, *args, **kwargs):
|
||||||
|
with self.dispatch_lock:
|
||||||
log.debug("Dispatching event {}".format(event))
|
log.debug("Dispatching event {}".format(event))
|
||||||
handle_method = '_'.join(('handle', event))
|
handle_method = '_'.join(('handle', event))
|
||||||
event_method = '_'.join(('on', event))
|
event_method = '_'.join(('on', event))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user