Skip to content

Commit 6f74063

Browse files
committed
DeviceServer: remove count arg; expect index in kwargs instead (issue #84)
Instead of having to explain the count/index special case in both serve_devices() and DeviceServer, limit it to serve_devices() by adding it to the device constructor arguments as soon as it is generated.
1 parent ee59b3d commit 6f74063

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

microscope/deviceserver.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,12 @@ def filter(self, record):
9393

9494

9595
class DeviceServer(multiprocessing.Process):
96-
def __init__(self, device_def, id_to_host, id_to_port, count=0,
97-
exit_event=None):
96+
def __init__(self, device_def, id_to_host, id_to_port, exit_event=None):
9897
"""Initialise a device and serve at host/port according to its id.
9998
10099
:param device_def: definition of the device
101100
:param id_to_host: host or mapping of device identifiers to hostname
102101
:param id_to_port: map or mapping of device identifiers to port number
103-
:param count: this is the countth process serving this class
104102
:param exit_event: a shared event to signal that the process
105103
should quit.
106104
"""
@@ -114,17 +112,14 @@ def __init__(self, device_def, id_to_host, id_to_port, count=0,
114112
self.exit_event = exit_event
115113
super(DeviceServer, self).__init__()
116114
self.daemon = True
117-
# Some SDKs need an index to access more than one device.
118-
self.count = count
119115

120116
def clone(self):
121117
"""Create new instance with same settings.
122118
123119
This is useful to restart a device server.
124120
"""
125121
return DeviceServer(self._device_def, self._id_to_host,
126-
self._id_to_port, exit_event=self.exit_event,
127-
count=self.count)
122+
self._id_to_port, exit_event=self.exit_event)
128123

129124
def run(self):
130125
logger = logging.getLogger(self._device_def['cls'].__name__)
@@ -148,7 +143,7 @@ def run(self):
148143
for def_key in ['cls', 'host', 'port', 'uid']:
149144
init_kwargs.pop(def_key)
150145

151-
self._device = self._device_def['cls'](index=self.count, **init_kwargs)
146+
self._device = self._device_def['cls'](**init_kwargs)
152147
while not self.exit_event.is_set():
153148
try:
154149
self._device.initialize()
@@ -263,9 +258,9 @@ def term_func(sig, frame):
263258
uid_to_port = None
264259

265260
for dev in devs:
266-
servers.append(DeviceServer(dev,
267-
uid_to_host, uid_to_port,
268-
exit_event=exit_event, count=count))
261+
dev['index'] = count
262+
servers.append(DeviceServer(dev, uid_to_host, uid_to_port,
263+
exit_event=exit_event))
269264
servers[-1].start()
270265
count += 1
271266

0 commit comments

Comments
 (0)