2Port of ROS2 `rclpy.task` for ROS1.
4------------------------------------------------------------------------------
5This file is part of rosros - simple unified interface to ROS1 / ROS2.
6Released under the BSD License.
11------------------------------------------------------------------------------
13## @namespace rosros.rclify.task
15# Original file copyright notice:
17# Copyright 2016 Open Source Robotics Foundation, Inc.
19# Licensed under the Apache License, Version 2.0 (the "License");
20# you may not use this file except in compliance with the License.
21# You may obtain a copy of the License at
23# http://www.apache.org/licenses/LICENSE-2.0
38 """Represents the outcome of a task in the future."""
40 def __init__(self, *, executor=None):
42 @param executor ignored (ROS2 API compatibility stand-
in)
60 print(
'The following exception was never retrieved: ' + str(self.
_exception),
64 """Yield while the task is not finished"""
70 """Request cancellation of the running task if it is not done already."""
78 Indicate if the task has been cancelled.
80 @return True if the task was cancelled
86 Indicate if the task has finished executing.
88 @return True if the task
is finished
or raised
while it was executing
94 Get the result of a done task.
96 @throws Exception
if one was set during the task.
98 @return The result set by the task
106 Get an exception raised by a done task.
108 @return the exception raised by the task
115 Set the result returned by a task.
117 @param result the output of a long running task
127 Set the exception raised by the task.
129 @param result the output of a long running task
140 Schedule done callbacks on the executor if possible,
else run them directly.
142 This function assumes self.
_lock is not held.
149 for callback
in callbacks:
152 except Exception
as e:
154 warnings.warn(
'Unhandled exception in done callback: {}'.format(e))
158 Add a callback to be executed when the task is done.
160 Callbacks should
not raise exceptions.
162 The callback may be called immediately by this method
if the future
is already done.
163 If this happens
and the callback raises, the exception will be raised by this method.
165 @param callback a callback taking the future
as an argument to be run when completed
181 Executes a function or coroutine.
183 This executes either a normal function
or a coroutine to completion. On completion it creates
184 tasks
for any
'done' callbacks.
187 def __init__(self, handler, args=None, kwargs=None, executor=None):
198 if inspect.iscoroutinefunction(handler):
209 Run or resume a task.
211 This attempts to execute a handler. If the handler
is a coroutine it will attempt to
212 await it. If there are done callbacks it will schedule them
with the executor.
214 The
return value of the handler
is stored
as the task result.
223 if inspect.iscoroutine(self.
_handler):
227 except StopIteration
as e:
232 except Exception
as e:
239 except Exception
as e:
248 """Cleanup after task finished."""
255 Check if the task
is currently being executed.
257 @return True if the task
is currently executing
262__all__ = [
"Future",
"Task"]
Represents the outcome of a task in the future.
set_exception(self, exception)
Set the exception raised by the task.
cancelled(self)
Indicate if the task has been cancelled.
_invoke_done_callbacks(self)
Schedule done callbacks on the executor if possible, else run them directly.
__init__(self, *executor=None)
set_result(self, result)
Set the result returned by a task.
add_done_callback(self, callback)
Add a callback to be executed when the task is done.
done(self)
Indicate if the task has finished executing.
exception(self)
Get an exception raised by a done task.
__await__(self)
Yield while the task is not finished.
cancel(self)
Request cancellation of the running task if it is not done already.
result(self)
Get the result of a done task.
Executes a function or coroutine.
__init__(self, handler, args=None, kwargs=None, executor=None)
__call__(self)
Run or resume a task.
executing(self)
Check if the task is currently being executed.
_complete_task(self)
Cleanup after task finished.