Skip to content

Commit 6d9503c

Browse files
Merge pull request #18 from jupyter-robotics/dev
Simplify asyncio syntax
2 parents a5793df + e51e38d commit 6d9503c

8 files changed

+36
-45
lines changed

content/introduction.ipynb

+15-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"metadata": {},
1515
"outputs": [],
1616
"source": [
17-
"%pip install ipynao==0.4.1"
17+
"%pip install ipynao==0.5.0"
1818
]
1919
},
2020
{
@@ -165,7 +165,7 @@
165165
},
166166
"outputs": [],
167167
"source": [
168-
"robot_summary = asyncio.ensure_future(motion.async_getSummary())"
168+
"robot_summary = motion.getSummary()"
169169
]
170170
},
171171
{
@@ -311,7 +311,7 @@
311311
"fps = 20\n",
312312
"sub_ID = \"jupy\"\n",
313313
"\n",
314-
"cam_sub = asyncio.ensure_future(vid.async_subscribeCamera(sub_ID, 0, resolution, color_space, fps))"
314+
"cam_sub = vid.subscribeCamera(sub_ID, 0, resolution, color_space, fps)"
315315
]
316316
},
317317
{
@@ -334,7 +334,7 @@
334334
},
335335
"outputs": [],
336336
"source": [
337-
"img_future = asyncio.ensure_future(vid.async_getImageRemote(sub_name))"
337+
"img_future = vid.getImageRemote(sub_name)"
338338
]
339339
},
340340
{
@@ -425,9 +425,9 @@
425425
"outputs": [],
426426
"source": [
427427
"async def little_dance():\n",
428-
" await motion.async_wakeUp()\n",
429-
" await animate.async_run(\"animations/Stand/Gestures/Hey_2\")\n",
430-
" await motion.async_rest()"
428+
" await motion.wakeUp()\n",
429+
" await animate.run(\"animations/Stand/Gestures/Hey_2\")\n",
430+
" await motion.rest()"
431431
]
432432
},
433433
{
@@ -470,9 +470,16 @@
470470
"outputs": [],
471471
"source": [
472472
"out = Output()\n",
473-
"res = asyncio.ensure_future(w.service(\"ALLeds\", out).async_rasta(\"gibberish\"))\n",
473+
"res = w.service(\"ALLeds\", out).rasta(\"gibberish\")\n",
474474
"out"
475475
]
476+
},
477+
{
478+
"cell_type": "code",
479+
"execution_count": null,
480+
"metadata": {},
481+
"outputs": [],
482+
"source": []
476483
}
477484
],
478485
"metadata": {

ipynao/_frontend.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
"""
1010

1111
module_name = "ipynao"
12-
module_version = "^0.4.1"
12+
module_version = "^0.5.0"

ipynao/nao_robot.py

+14-25
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from ipywidgets import DOMWidget, Output
1212
from traitlets import Unicode, Integer
1313
from ._frontend import module_name, module_version
14-
import asyncio
14+
from asyncio import ensure_future, Future
1515

1616

1717
class NaoRobotService():
@@ -36,33 +36,20 @@ def _create_msg(self, method_name, *args, **kwargs):
3636
self.widget.request_id += 1
3737
return data
3838

39-
def call_service(self, method_name, *args, **kwargs):
40-
data = self._create_msg(method_name, *args, **kwargs)
41-
self.widget.send(data)
4239

43-
async def async_call_service(self, method_name, *args, **kwargs):
40+
async def call_service(self, method_name, *args, **kwargs):
4441
data = self._create_msg(method_name, *args, **kwargs)
4542
self.widget.send(data)
4643
request_id = data['requestID']
4744

48-
try:
49-
self.output.clear_output()
50-
self.output.append_stdout('Calling service... \n')
51-
await self.widget.wait_for_change('counter', self.output, request_id)
52-
except Exception as e:
53-
return e
54-
55-
response = self.widget.response[request_id]['data']
56-
del self.widget.response[request_id]
45+
self.output.append_stdout(f'Calling service {self.name}...\n')
46+
future = await self.widget.wait_for_change('counter', self.output, request_id)
5747

58-
return response
48+
return future
5949

6050

6151
def __getattr__(self, method_name):
62-
if (method_name[:6] == 'async_'):
63-
return lambda *x, **y: self.async_call_service(method_name[6:], *x, **y)
64-
else:
65-
return lambda *x, **y: self.call_service(method_name, *x, **y)
52+
return lambda *x, **y: ensure_future(self.call_service(method_name, *x, **y))
6653

6754

6855
class NaoRobotWidget(DOMWidget):
@@ -95,7 +82,7 @@ def _handle_frontend_msg(self, model, msg, buffer):
9582

9683

9784
def wait_for_change(widget, value_name, output=Output(), request_id=0):
98-
future = asyncio.Future()
85+
future = Future()
9986
widget.response[request_id] = {
10087
'isError': False,
10188
'data': None
@@ -108,17 +95,19 @@ def get_value_change(change):
10895
widget.unobserve(get_value_change, names=value_name)
10996

11097
if (response['isError']):
111-
future.set_exception(Exception(response['data']))
98+
if not future.done():
99+
# TODO: Fix "Task exception was never retrieved"
100+
# future.set_exception(Exception(response['data']))
101+
future.set_result(Exception(response['data']))
112102
output.append_stderr(str(response['data']) + '\n')
113103
else:
114-
future.set_result(response['data'])
104+
if not future.done():
105+
future.set_result(response['data'])
115106
output.append_stdout(str(response['data']) + '\n')
116107

117-
else:
118-
future.set_result(change)
119108

120109
widget.observe(get_value_change, names=value_name)
121-
return future
110+
return future
122111

123112

124113
def connect(self, ip_address='nao.local', port='80'):

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ipynao",
3-
"version": "0.4.1",
3+
"version": "0.5.0",
44
"description": "A widget library for controlling Nao",
55
"keywords": [
66
"jupyter",

pyproject.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ classifiers = [
3434
dependencies = [
3535
"ipywidgets>=7.0.0",
3636
]
37-
version = "0.4.1"
37+
version = "0.5.0"
3838

3939
[project.optional-dependencies]
4040
docs = [
@@ -104,7 +104,7 @@ file = [
104104
]
105105

106106
[tool.tbump.version]
107-
current = "0.4.1"
107+
current = "0.5.0"
108108
regex = "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)((?P<channel>a|b|rc|.dev)(?P<release>\\d+))?"
109109

110110
[tool.tbump.git]

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ ipympl>=0.8.2
2727
ipycanvas>=0.9.1
2828

2929
# Python: ipynao library for Nao robot
30-
ipynao>=0.4.1
30+
ipynao>=0.5.0
3131

3232
# For examples with images
3333
Pillow

src/widget.ts

-5
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,6 @@ export class NaoRobotModel extends DOMWidgetModel {
155155
// the request ID is the next one which is used to call the service
156156
const naoService = await servicePromise
157157
.then((resolution: any) => {
158-
this.send({
159-
isError: false,
160-
data: true, // TODO: resolution ?? true,
161-
requestID: requestID + 1, // Note above
162-
});
163158
return resolution;
164159
})
165160
.catch((rejection: string) => {

0 commit comments

Comments
 (0)