本文整理汇总了Python中odemis.model.CancellableThreadPoolExecutor.submitf方法的典型用法代码示例。如果您正苦于以下问题:Python CancellableThreadPoolExecutor.submitf方法的具体用法?Python CancellableThreadPoolExecutor.submitf怎么用?Python CancellableThreadPoolExecutor.submitf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类odemis.model.CancellableThreadPoolExecutor
的用法示例。
在下文中一共展示了CancellableThreadPoolExecutor.submitf方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: AntiBacklashActuator
# 需要导入模块: from odemis.model import CancellableThreadPoolExecutor [as 别名]
# 或者: from odemis.model.CancellableThreadPoolExecutor import submitf [as 别名]
#.........这里部分代码省略.........
try:
f.result(timeout=0.01)
except futures.TimeoutError:
pass # Keep waiting for end of move
else:
done = True
# Check if there is already a new move to do
nf = self._executor.get_next_future(future)
if nf is not None and axes <= nf._update_axes:
logging.debug("Ending move control early as next move is an update containing %s", axes)
return
# backlash move
self._antiBacklashMove(shift.keys())
def _doMoveAbs(self, future, pos):
sub_pos = {}
for a, v in pos.items():
if a not in self._backlash:
sub_pos[a] = v
else:
shift = v - self.position.value[a]
with self._shifted_lock:
if shift * self._backlash[a] >= 0:
sub_pos[a] = v
self._shifted[a] = False
else:
sub_pos[a] = v - self._backlash[a]
self._shifted[a] = True
# Do the backlash + move
axes = set(pos.keys())
if not any(self._shifted):
# a tiny bit faster as we don't sleep
self._child.moveAbsSync(sub_pos)
else: # some antibacklash move needed afterwards => update might be worthy
f = self._child.moveAbs(sub_pos)
done = False
while not done:
try:
f.result(timeout=0.01)
except futures.TimeoutError:
pass # Keep waiting for end of move
else:
done = True
# Check if there is already a new move to do
nf = self._executor.get_next_future(future)
if nf is not None and axes <= nf._update_axes:
logging.debug("Ending move control early as next move is an update containing %s", axes)
return
# anti-backlash move
self._antiBacklashMove(axes)
def _createFuture(self, axes, update):
"""
Return (CancellableFuture): a future that can be used to manage a move
axes (set of str): the axes that are moved
update (bool): if it's an update move
"""
# TODO: do this via the __init__ of subclass of Future?
f = CancellableFuture() # TODO: make it cancellable too
f._update_axes = set() # axes handled by the move, if update
if update:
# Check if all the axes support it
if all(self.axes[a].canUpdate for a in axes):
f._update_axes = axes
else:
logging.warning("Trying to do a update move on axes %s not supporting update", axes)
return f
@isasync
def moveRel(self, shift, update=False):
if not shift:
return model.InstantaneousFuture()
self._checkMoveRel(shift)
f = self._createFuture(set(shift.keys()), update)
return self._executor.submitf(f, self._doMoveRel, f, shift)
@isasync
def moveAbs(self, pos, update=False):
if not pos:
return model.InstantaneousFuture()
self._checkMoveAbs(pos)
f = self._createFuture(set(pos.keys()), update)
return self._executor.submitf(f, self._doMoveAbs, f, pos)
def stop(self, axes=None):
self._child.stop(axes=axes)
@isasync
def reference(self, axes):
f = self._child.reference(axes)
return f
示例2: PM8742
# 需要导入模块: from odemis.model import CancellableThreadPoolExecutor [as 别名]
# 或者: from odemis.model.CancellableThreadPoolExecutor import submitf [as 别名]
#.........这里部分代码省略.........
self.SetVelocity(i, sps)
return value
def _createFuture(self):
"""
Return (CancellableFuture): a future that can be used to manage a move
"""
f = CancellableFuture()
f._moving_lock = threading.Lock() # taken while moving
f._must_stop = threading.Event() # cancel of the current future requested
f._was_stopped = False # if cancel was successful
f.task_canceller = self._cancelCurrentMove
return f
@isasync
def moveRel(self, shift):
self._checkMoveRel(shift)
shift = self._applyInversion(shift)
# Check if the distance is big enough to make sense
for an, v in shift.items():
aid = self._name_to_axis[an]
if abs(v) < self._stepsize[aid - 1]:
# TODO: store and accumulate all the small moves instead of dropping them?
del shift[an]
logging.info("Dropped too small move of %g m < %g m",
abs(v), self._stepsize[aid - 1])
if not shift:
return model.InstantaneousFuture()
f = self._createFuture()
f = self._executor.submitf(f, self._doMoveRel, f, shift)
return f
@isasync
def moveAbs(self, pos):
if not pos:
return model.InstantaneousFuture()
self._checkMoveAbs(pos)
pos = self._applyInversion(pos)
f = self._createFuture()
f = self._executor.submitf(f, self._doMoveAbs, f, pos)
return f
moveAbs.__doc__ = model.Actuator.moveAbs.__doc__
def stop(self, axes=None):
self._executor.cancel()
def _doMoveRel(self, future, pos):
"""
Blocking and cancellable relative move
future (Future): the future it handles
pos (dict str -> float): axis name -> relative target position
"""
with future._moving_lock:
end = 0 # expected end
moving_axes = set()
for an, v in pos.items():
aid = self._name_to_axis[an]
moving_axes.add(aid)
steps = int(round(v / self._stepsize[aid - 1]))
self.MoveRel(aid, steps)
# compute expected end
示例3: Chamber
# 需要导入模块: from odemis.model import CancellableThreadPoolExecutor [as 别名]
# 或者: from odemis.model.CancellableThreadPoolExecutor import submitf [as 别名]
#.........这里部分代码省略.........
"Simulated pressure update")
self._press_timer.start()
else:
self._press_timer = None
# Indicates whether the chamber is opened or not
# Just pretend it's always closed, and allow the user to change that
# for instance via CLI.
self.opened = model.BooleanVA(False)
# will take care of executing axis move asynchronously
self._executor = CancellableThreadPoolExecutor(max_workers=1) # one task at a time
def terminate(self):
if self._press_timer:
self._press_timer.cancel()
self._press_timer = None
if self._executor:
self.stop()
self._executor.shutdown()
self._executor = None
def _updatePressure(self):
"""
update the pressure VA (called regularly from a thread)
"""
# Compute the current pressure
now = time.time()
if self._time_goal < now: # done
# goal ±5%
pos = self._goal * random.uniform(0.95, 1.05)
else:
# TODO make it logarithmic
ratio = (now - self._time_start) / (self._time_goal - self._time_start)
pos = self._position + (self._goal - self._position) * ratio
# it's read-only, so we change it via _value
self.pressure._value = pos
self.pressure.notify(pos)
def _updatePosition(self):
"""
update the position VA
"""
# .position contains the last known/valid position
# it's read-only, so we change it via _value
self.position._value = {"pressure": self._position}
self.position.notify(self.position.value)
@isasync
def moveRel(self, shift):
self._checkMoveRel(shift)
# convert into an absolute move
pos = {}
for a, v in shift.items:
pos[a] = self.position.value[a] + v
return self.moveAbs(pos)
@isasync
def moveAbs(self, pos):
if not pos:
return model.InstantaneousFuture()
self._checkMoveAbs(pos)
new_pres = pos["pressure"]
est_start = time.time() + 0.1
f = model.ProgressiveFuture(start=est_start,
end=est_start + self._getDuration(new_pres))
return self._executor.submitf(f, self._changePressure, f, new_pres)
def _getDuration(self, pos):
return abs(self._position - pos) / SPEED_PUMP
def _changePressure(self, f, p):
"""
Synchronous change of the pressure
p (float): target pressure
"""
# TODO: allow to cancel during the change
now = time.time()
duration = self._getDuration(p) # s
self._time_start = now
self._time_goal = now + duration # s
self._goal = p
time.sleep(duration / 2)
# DEBUG: for testing wrong time estimation
# f.set_progress(start=self._time_start, end=self._time_goal + 10)
time.sleep(duration / 2)
self._position = p
self._updatePosition()
def stop(self, axes=None):
self._executor.cancel()
logging.warning("Stopped pressure change")
示例4: TMCM3110
# 需要导入模块: from odemis.model import CancellableThreadPoolExecutor [as 别名]
# 或者: from odemis.model.CancellableThreadPoolExecutor import submitf [as 别名]
#.........这里部分代码省略.........
self.temperature._value = t0
self.temperature.notify(t0)
self.temperature1._value = t1
self.temperature1.notify(t1)
def _createFuture(self):
"""
Return (CancellableFuture): a future that can be used to manage a move
"""
f = CancellableFuture()
f._moving_lock = threading.Lock() # taken while moving
f._must_stop = threading.Event() # cancel of the current future requested
f._was_stopped = False # if cancel was successful
f.task_canceller = self._cancelCurrentMove
return f
@isasync
def moveRel(self, shift):
self._checkMoveRel(shift)
shift = self._applyInversionRel(shift)
# Check if the distance is big enough to make sense
for an, v in shift.items():
aid = self._axes_names.index(an)
if abs(v) < self._ustepsize[aid]:
# TODO: store and accumulate all the small moves instead of dropping them?
del shift[an]
logging.info("Dropped too small move of %f m", abs(v))
if not shift:
return model.InstantaneousFuture()
f = self._createFuture()
f = self._executor.submitf(f, self._doMoveRel, f, shift)
return f
@isasync
def moveAbs(self, pos):
if not pos:
return model.InstantaneousFuture()
self._checkMoveAbs(pos)
pos = self._applyInversionRel(pos)
f = self._createFuture()
f = self._executor.submitf(f, self._doMoveAbs, f, pos)
return f
moveAbs.__doc__ = model.Actuator.moveAbs.__doc__
@isasync
def reference(self, axes):
if not axes:
return model.InstantaneousFuture()
self._checkReference(axes)
f = self._executor.submit(self._doReference, axes)
return f
reference.__doc__ = model.Actuator.reference.__doc__
def stop(self, axes=None):
self._executor.cancel()
def _doMoveRel(self, future, pos):
"""
Blocking and cancellable relative move
future (Future): the future it handles
pos (dict str -> float): axis name -> relative target position
示例5: Stage
# 需要导入模块: from odemis.model import CancellableThreadPoolExecutor [as 别名]
# 或者: from odemis.model.CancellableThreadPoolExecutor import submitf [as 别名]
#.........这里部分代码省略.........
# not moving.
moving = True
tstart = time.time()
while moving:
x, y, z, moving = self.parent.GetStagePosition()
# Take the opportunity to update .position
self._updatePosition({"x": x, "y": y, "z": z})
if time.time() > tstart + timeout:
self.parent.Abort()
logging.error("Timeout after submitting stage move. Aborting move.")
break
# 50 ms is about the time it takes to read the stage status
time.sleep(50e-3)
# If it was cancelled, Abort() has stopped the stage before, and
# we still have waited until the stage stopped moving. Now let
# know the user that the move is not complete.
if future._must_stop.is_set():
raise CancelledError()
except RemconError:
if future._must_stop.is_set():
raise CancelledError()
raise
finally:
future._was_stopped = True
# Update the position, even if the move didn't entirely succeed
self._updatePosition()
def _cancelCurrentMove(self, future):
"""
Cancels the current move (both absolute or relative). Non-blocking.
future (Future): the future to stop. Unused, only one future must be
running at a time.
return (bool): True if it successfully cancelled (stopped) the move.
"""
# The difficulty is to synchronise correctly when:
# * the task is just starting (not finished requesting axes to move)
# * the task is finishing (about to say that it finished successfully)
logging.debug("Cancelling current move")
future._must_stop.set() # tell the thread taking care of the move it's over
self.parent.Abort()
with future._moving_lock:
if not future._was_stopped:
logging.debug("Cancelling failed")
return future._was_stopped
def _createFuture(self):
"""
Return (CancellableFuture): a future that can be used to manage a move
"""
f = CancellableFuture()
f._moving_lock = threading.Lock() # taken while moving
f._must_stop = threading.Event() # cancel of the current future requested
f._was_stopped = False # if cancel was successful
f.task_canceller = self._cancelCurrentMove
return f
@isasync
def moveRel(self, shift):
"""
shift (dict): shift in m
"""
if not shift:
return model.InstantaneousFuture()
self._checkMoveRel(shift)
shift = self._applyInversion(shift)
f = self._createFuture()
f = self._executor.submitf(f, self._doMoveRel, f, shift)
return f
@isasync
def moveAbs(self, pos):
"""
pos (dict): position in m
"""
if not pos:
return model.InstantaneousFuture()
self._checkMoveAbs(pos)
pos = self._applyInversion(pos)
f = self._createFuture()
f = self._executor.submitf(f, self._doMoveAbs, f, pos)
return f
def stop(self, axes=None):
# Empty the queue (and already stop the stage if a future is running)
self._executor.cancel()
# Try to stop the stage, even if no future is running, for safety
logging.warning("Stopping all axes: %s", ", ".join(self.axes))
self.parent.Abort()
try:
self._updatePosition()
except Exception:
logging.exception("Unexpected failure when updating position")
示例6: ESP
# 需要导入模块: from odemis.model import CancellableThreadPoolExecutor [as 别名]
# 或者: from odemis.model.CancellableThreadPoolExecutor import submitf [as 别名]
#.........这里部分代码省略.........
"""
High level commands (ie, Odemis Actuator API)
"""
def _applyOffset(self, pos):
"""
Apply the offset to the position and return it
"""
ret = dict(pos)
for axis in self._offset:
if axis in ret:
ret[axis] -= self._offset[axis]
return ret
def _removeOffset(self, pos):
"""
Remove the offset from the position and return it
"""
ret = dict(pos)
for axis in self._offset:
if axis in ret:
ret[axis] += self._offset[axis]
return ret
@isasync
def moveAbs(self, pos):
if not pos:
return model.InstantaneousFuture()
pos = self._removeOffset(pos) # Get the position in controller coord.
self._checkMoveAbs(pos)
pos = self._applyInversion(pos)
f = self._createMoveFuture()
f = self._executor.submitf(f, self._doMoveAbs, f, pos)
return f
@isasync
def moveRel(self, shift):
self._checkMoveRel(shift)
shift = self._applyInversion(shift)
f = self._createMoveFuture()
f = self._executor.submitf(f, self._doMoveRel, f, shift)
return f
def _doMoveRel(self, future, pos):
"""
Blocking and cancellable relative move
future (Future): the future it handles
_pos (dict str -> float): axis name -> relative target position
raise:
ValueError: if the target position is
TMCLError: if the controller reported an error
CancelledError: if cancelled before the end of the move
"""
with future._moving_lock:
end = 0 # expected end
moving_axes = set()
for an, v in pos.items():
aid = self._axis_map[an]
moving_axes.add(aid)
self.MoveRelPos(aid, v * self._axis_conv_factor[aid])
# compute expected end
# convert to mm units
dur = driver.estimateMoveDuration(abs(v) * self._axis_conv_factor[aid],
self._speed[an],
self._accel[an])