@@ -98,7 +98,7 @@ def __init__(self, streaming=False):
98
98
self .msgid = 0
99
99
self .msgbuf = {}
100
100
self .msgio = MsgPackIO () if streaming else None
101
- self .servers = []
101
+ self .callables = {}
102
102
103
103
def _bind_callback (self , src , name ):
104
104
if log_level_enabled (logging .INFO ):
@@ -135,30 +135,31 @@ def _send_msg(self, msgid, msgtype, fname, fargs, **kwargs):
135
135
return Future (msgid , self .msgbuf , fname , fargs )
136
136
137
137
def _dispatch (self , msgid , fname , fargs ):
138
- func = None
139
138
retobj = None
140
139
error = None
141
- for obj in self .servers :
142
- if callable (obj ) and obj .__name__ == fname :
143
- func = obj
144
- elif hasattr (obj , fname ):
145
- func = getattr (obj , fname )
146
- if func is not None :
147
- break
148
-
149
- if func is not None :
150
- retobj = func (* fargs )
140
+
141
+ if fname in self .callables :
142
+ retobj = self .callables [fname ](* fargs )
151
143
else :
152
144
error = "Unbound function called %s" % (fname )
153
145
154
146
self ._send_msg (msgid , _MSG_TYPE_RESPONSE , error , retobj )
155
147
156
- def bind (self , obj ):
148
+ def bind (self , name , obj ):
157
149
"""
158
- Register an object or a function to be called by the remote processor.
159
- obj: An object whose methods can be called by remote processors, or a function.
150
+ Bind a callable or an object to a name.
151
+ name: The name to which the callable or object is bound.
152
+ obj: A callable or an object to bind to the name. If an object is passed, all of its
153
+ public methods will be bound to their respective qualified names.
160
154
"""
161
- self .servers .append (obj )
155
+ if callable (obj ):
156
+ # Bind a single callable to its name.
157
+ self .callables [name ] = obj
158
+ else :
159
+ # Bind all public methods of an object to their respective qualified names.
160
+ for k , v in obj .__class__ .__dict__ .items ():
161
+ if callable (v ) and not k .startswith ("_" ):
162
+ self .callables [name + "." + k ] = getattr (obj , k )
162
163
163
164
def start (self , firmware = None , num_channels = 2 , timeout = 3000 ):
164
165
"""
0 commit comments