Skip to content

Commit 4115ff5

Browse files
committed
Rework "Waiting for messages" section in README
* README.org: Do it.
1 parent b691d38 commit 4115ff5

File tree

1 file changed

+15
-35
lines changed

1 file changed

+15
-35
lines changed

README.org

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,18 +1307,22 @@ set the =jupyter-request-inhibited-handlers= slot of a =jupyter-request=
13071307
object. This slot can take the same values as =jupyter-inhibit-handlers=.
13081308
** Waiting for messages
13091309

1310-
All message passing between the kernel and Emacs happens asynchronously. So if
1311-
a code path in Emacs Lisp is dependent on some message already having been
1312-
received, e.g. an idle message, there needs to be primitives that will block so
1313-
that there is a guarantee that a particular message has been received before
1314-
proceeding.
1310+
When using an asynchronous method of communication between a kernel and a
1311+
client, e.g. when =jupyter-ioloop-comm= is used, there needs to be a way for a
1312+
program to guarantee that some message has already been received. That is, a
1313+
program must be able to wait for particular messages before proceeding.
13151314

1316-
The following functions all wait for different conditions to be met on the
1317-
received messages of a request and return the message that caused the function
1318-
to stop waiting or =nil= if no message was received within a timeout period.
1319-
The default timeout is =jupyter-default-timeout= seconds.
1315+
=jupyter-wait-until= will wait until the function it is passed returns non-nil
1316+
for one of the messages received by a request and will return either the value
1317+
of the function or =nil= if a timeout is reached. The default timeout
1318+
is =jupyter-default-timeout=.
13201319

1321-
For example, to wait until an idle message has been received for a request:
1320+
=jupyter-wait-until-received= and =jupyter-wait-until-idle= are convenience
1321+
functions built on top of =jupyter-wait-until=.
1322+
1323+
*** Examples
1324+
1325+
Wait until an idle message has been received for a request:
13221326

13231327
#+BEGIN_SRC elisp
13241328
(let ((timeout 4))
@@ -1328,37 +1332,13 @@ For example, to wait until an idle message has been received for a request:
13281332
timeout))
13291333
#+END_SRC
13301334

1331-
To wait until a message of a specific type is received for a request:
1335+
Wait until a message of a specific type is received for a request:
13321336

13331337
#+BEGIN_SRC elisp
13341338
(jupyter-wait-until-received :execute-reply
13351339
(jupyter-send-execute-request client :code "[i*10 for i in range(100000)]"))
13361340
#+END_SRC
13371341

1338-
The most general form of the blocking functions is =jupyter-wait-until= which
1339-
takes a message type and a predicate function of a single argument. Whenever a
1340-
message is received that matches the message type, the message is passed to the
1341-
function to determine if =jupyter-wait-until= should return from waiting.
1342-
1343-
#+BEGIN_SRC elisp
1344-
(defun stream-prints-50-p (msg)
1345-
(let ((text (jupyter-message-get msg :text)))
1346-
(cl-loop for line in (split-string text "\n")
1347-
thereis (equal line "50"))))
1348-
1349-
(let ((timeout 2))
1350-
(jupyter-wait-until
1351-
(jupyter-send-execute-request client :code "[print(i) for i in range(100)]")
1352-
:stream #'stream-prints-50-p
1353-
timeout))
1354-
#+END_SRC
1355-
1356-
The above code runs =stream-prints-50-p= for every =stream= message received
1357-
from a kernel (here assumed to be a python kernel) for an execute request that
1358-
prints the numbers 0 to 99 and waits until the kernel has printed the number 50
1359-
before returning from the =jupyter-wait-until= call. If the number 50 is not
1360-
printed before the two second timeout, =jupyter-wait-until= returns =nil=.
1361-
Otherwise it returns the stream message whose content contains the number 50.
13621342
** Message property lists
13631343
:PROPERTIES:
13641344
:ID: D09FDD89-43A9-41DA-A6E8-6D6C73336981

0 commit comments

Comments
 (0)