Skip to content

Commit 53db751

Browse files
committed
Remove chain_future
1 parent 697dd25 commit 53db751

File tree

3 files changed

+5
-59
lines changed

3 files changed

+5
-59
lines changed

rclpy/rclpy/executors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ async def _execute() -> None:
275275
# The request was cancelled
276276
pass
277277
else:
278-
if not future._executor():
278+
if isinstance(future, Future) and not future._executor():
279279
future._set_executor(self)
280280

281281
future.set_result(response)

rclpy/rclpy/experimental/asyncio_executor.py

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,10 @@
2323
EntityT = TypeVar("EntityT", bound=Union[Subscription, Service, Client, Timer])
2424

2525

26-
def _chain_future(rclpy_future: rclpy.Future, asyncio_future: asyncio.Future) -> None:
27-
"""Chain two futures so that when one completes, so does the other.
28-
29-
The result (or exception) of source will be copied to destination.
30-
If destination is cancelled, source gets cancelled too.
31-
"""
32-
33-
def _call_check_cancel(_: asyncio.Future):
34-
if asyncio_future.cancelled():
35-
rclpy_future.cancel()
36-
37-
def _call_set_state(_: rclpy.Future):
38-
if asyncio_future.cancelled():
39-
return
40-
if rclpy_future.cancelled():
41-
asyncio_future.cancel()
42-
else:
43-
exception = rclpy_future.exception()
44-
if exception is not None:
45-
asyncio_future.set_exception(exception)
46-
else:
47-
result = rclpy_future.result()
48-
asyncio_future.set_result(result)
49-
50-
asyncio_future.add_done_callback(_call_check_cancel)
51-
rclpy_future.add_done_callback(_call_set_state)
52-
5326
def _is_timer_destroyed(timer: Timer):
5427
return timer.handle.pointer == 0
5528

29+
5630
class AsyncioExecutor(BaseExecutor[asyncio.Future, asyncio.Task]):
5731
def __init__(
5832
self,
@@ -207,12 +181,9 @@ def remove_node(self, node: Node) -> None:
207181
self._nodes.remove(node)
208182
self._update_entities_from_nodes()
209183

210-
def create_future(self, *, from_future: Optional[rclpy.Future] = None) -> asyncio.Future:
184+
def create_future(self) -> asyncio.Future:
211185
asyncio_future = self._loop.create_future()
212186

213-
if from_future:
214-
_chain_future(from_future, asyncio_future)
215-
216187
return asyncio_future
217188

218189
# TODO: optimize this function to run less times?

rclpy/test/test_asyncio_executor.py

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -73,31 +73,6 @@ async def test_coro():
7373
asyncio.run(test_coro())
7474

7575

76-
def test_wrapped_future_is_done_when_future_is_done(executor):
77-
ros_fut = Future()
78-
79-
async def test_coro():
80-
return await executor.create_future(from_future=ros_fut)
81-
82-
task = executor.create_task(test_coro())
83-
executor.loop.call_soon(ros_fut.set_result, "finished")
84-
85-
executor.spin_until_future_complete(task, timeout=0.3)
86-
assert task.result() == "finished"
87-
88-
89-
def test_wrapped_future_is_cancelled_when_future_is_cancelled(executor):
90-
ros_fut = Future()
91-
92-
async def test_coro():
93-
return await executor.create_future(from_future=ros_fut)
94-
95-
task = executor.create_task(test_coro())
96-
executor.loop.call_soon(ros_fut.cancel)
97-
executor.spin_until_future_complete(task, timeout=0.3)
98-
assert task.cancelled()
99-
100-
10176
def test_spin_once_returns_after_callback(executor, attached_test_node):
10277
mock = Mock()
10378
msg = String(data="test")
@@ -163,7 +138,7 @@ def cb(request, response):
163138
attached_test_node.create_service(BasicTypes, '/test_srv', cb)
164139
client = attached_test_node.create_client(BasicTypes, '/test_srv')
165140
fut = client.call_async(BasicTypes.Request(bool_value=True))
166-
executor.spin_until_future_complete(executor.create_future(from_future=fut), timeout=0.3)
141+
executor.spin_until_future_complete(fut, timeout=0.3)
167142
assert fut.result().string_value == "True"
168143

169144

@@ -201,7 +176,7 @@ def service_cb(req, resp):
201176
with attach_to_executor(test_node, executor):
202177
req = BasicTypes.Request()
203178
fut = client.call_async(req)
204-
executor.spin_until_future_complete(executor.create_future(from_future=fut), timeout=0.3)
179+
executor.spin_until_future_complete(fut, timeout=0.3)
205180
assert fut.result().bool_value is True
206181

207182
fut2 = client.call_async(BasicTypes.Request())

0 commit comments

Comments
 (0)