You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This function is called after the server's event loop is running.
87
+
"""
88
+
await asyncio.sleep(.1)
89
+
90
+
.. note:: The server startup banner (displaying server info and access URLs) is printed before starting asynchronous tasks, so those tasks might still be running even after the banner appears.
91
+
92
+
.. WARNING: This note is also present in the "Starting asynchronous tasks from an ExtensionApp" section.
93
+
If you update it here, please update it there as well.
94
+
68
95
Making an extension discoverable
69
96
--------------------------------
70
97
@@ -117,6 +144,7 @@ An ExtensionApp:
117
144
- has an entrypoint, ``jupyter <name>``.
118
145
- can serve static content from the ``/static/<name>/`` endpoint.
119
146
- can add new endpoints to the Jupyter Server.
147
+
- can start asynchronous tasks after the server has started.
120
148
121
149
The basic structure of an ExtensionApp is shown below:
122
150
@@ -156,6 +184,11 @@ The basic structure of an ExtensionApp is shown below:
156
184
...
157
185
# Change the jinja templating environment
158
186
187
+
asyncdef_start_jupyter_server_extension(self):
188
+
...
189
+
# Extend this method to start any (e.g. async) tasks
190
+
# after the main Server's Event Loop is running.
191
+
159
192
asyncdefstop_extension(self):
160
193
...
161
194
# Perform any required shut down steps
@@ -171,6 +204,7 @@ Methods
171
204
* ``initialize_settings()``: adds custom settings to the Tornado Web Application.
172
205
* ``initialize_handlers()``: appends handlers to the Tornado Web Application.
173
206
* ``initialize_templates()``: initialize the templating engine (e.g. jinja2) for your frontend.
207
+
* ``_start_jupyter_server_extension()``: enables the extension to start (async) tasks _after_ the server's main Event Loop has started.
174
208
* ``stop_extension()``: called on server shut down.
175
209
176
210
Properties
@@ -320,6 +354,48 @@ pointing at the ``load_classic_server_extension`` method:
320
354
If the extension is enabled, the extension will be loaded when the server starts.
321
355
322
356
357
+
Starting asynchronous tasks from an ExtensionApp
358
+
------------------------------------------------
359
+
360
+
.. versionadded:: 2.15.0
361
+
362
+
363
+
An ``ExtensionApp`` can start asynchronous tasks after Jupyter Server's event loop is started by overriding its
364
+
``_start_jupyter_server_extension()`` method. This can be helpful for setting up e.g. background tasks.
365
+
366
+
Here is a basic (pseudo) code example:
367
+
368
+
.. code-block:: python
369
+
370
+
import asyncio
371
+
import time
372
+
373
+
374
+
asyncdeflog_time_periodically(log, dt=1):
375
+
"""Log the current time from a periodic loop."""
376
+
whileTrue:
377
+
current_time = time.time()
378
+
log.info(current_time)
379
+
await sleep(dt)
380
+
381
+
382
+
classMyExtension(ExtensionApp):
383
+
...
384
+
385
+
asyncdef_start_jupyter_server_extension(self):
386
+
self.my_background_task = asyncio.create_task(
387
+
log_time_periodically(self.log)
388
+
)
389
+
390
+
asyncdefstop_extension(self):
391
+
self.my_background_task.cancel()
392
+
393
+
.. note:: The server startup banner (displaying server info and access URLs) is printed before starting asynchronous tasks, so those tasks might still be running even after the banner appears.
394
+
395
+
.. WARNING: This note is also present in the "Starting asynchronous tasks from an extension" section.
396
+
If you update it here, please update it there as well.
0 commit comments