@@ -109,17 +109,18 @@ def unwatch(self, c, callback=None):
109109
110110 debug ("Unwatching command: %s" % str (c ))
111111
112- # if a callback was specified, only remove the callback
113- if hasattr (callback , "__call__" ) and (callback in self .callbacks [c ]):
114- self .callbacks [c ].remove (callback )
115-
116- # if no more callbacks are left, remove the command entirely
117- if len (self .callbacks [c ]) == = 0 :
112+ if c in self .commands :
113+ # if a callback was specified, only remove the callback
114+ if hasattr (callback , "__call__" ) and (callback in self .callbacks [c ]):
115+ self .callbacks [c ].remove (callback )
116+
117+ # if no more callbacks are left, remove the command entirely
118+ if len (self .callbacks [c ]) == 0 :
119+ self .commands .pop (c , None )
120+ else :
121+ # no callback was specified, pop everything
122+ self .callbacks .pop (c , None )
118123 self .commands .pop (c , None )
119- else :
120- # no callback was specified, pop everything
121- self .callbacks .pop (c , None )
122- self .commands .pop (c , None )
123124
124125
125126 # start if neccessary
@@ -128,10 +129,19 @@ def unwatch(self, c, callback=None):
128129
129130
130131 def unwatch_all (self ):
131- commands = self .commands .keys ()
132+ debug ("Unwatching all" )
133+
134+ # if running, the daemon thread must be stopped before altering the command dict
135+ was_running = self .running
136+ if self .running :
137+ self .stop ()
138+
139+ self .commands = {}
140+ self .callbacks = {}
132141
133- for c in commands :
134- self .unwatch (c )
142+ # start if neccessary
143+ if was_running :
144+ self .start ()
135145
136146
137147 def query (self , c ):
@@ -156,8 +166,8 @@ def run(self):
156166 self .commands [c ] = r
157167
158168 # fire the callback, if we have one
159- if c in self .callbacks :
160- self . callbacks [ c ] (r )
169+ for callback in self .callbacks [ c ] :
170+ callback (r )
161171
162172 else :
163173 time .sleep (1 ) # idle until the user calls stop() or watch()
0 commit comments