The issue you're running into is that the Jupyter protocol is fully serial.
I've opened a GitHub issue/RFC
<https://github.com/jupyter/jupyter_client/issues/433> that would make some
things not-serial, but alas, the state of the world is that things *are*
 serial.

So, essentially, what happens is this:

   - You click run all in the UI
   - The frontend sends 3 *execute_request* messages to the Jupyter server
   (and ultimately the kernel)
   - The kernel starts executing request one (the remaining 2 are queued)
   - The JavaScript is sent to the frontend which sends another
   *execute_request* message.
      - This new *execute_request* is queued behind the other 2 that were
      pending before
   - The kernel finishes request one, and starts request two
   - ...then request three...
   - Finally, after request three, the kernel handles the
*execute_request* that
   was initiated by your JavaScript code

>From the kernel's perspective, it executes everything in order.

I don't see this being likely to change. The kernel protocol is actually
architected in a way to allow concurrent users, so the concept of sending a
request to the frontend is ill-defined since there may be zero, one, or
more frontends actively connected.

On Thu, Jun 25, 2020 at 2:55 PM Barry Demchak <[email protected]> wrote:

> Hi --
>
> I'm trying to get values from Javascript to a running Notebook ... think
> of the values as telemetry data coming from the client browser/PC.
>
> The idea is for the Notebook to trigger the Javascript in one cell and
> then chew on the data in Python (3.7) in another cell.
>
> This works when I single step the cells, but not when I run all cells in
> one swipe.
>
> I think the reason is that the kernel execute/shell queue is serviced only
> once the kernel goes idle. When I'm single stepping through the cells,
> kernel idle would occur at the end of each cell. But when I'm running all
> cells, idle doesn't occur until after I needed the value. Bad news.
>
> Here are my three cells (as a toy example, actual notebook file attached):
>
> *#1:*
>
>
>
>
>
>
>
> *from IPython.display import HTMLjavascript = f'''<script
> type="text/Javascript">var my_var = 10</script>'''HTML(javascript)*
>
>
> *#2:*
>
>
>
>
>
>
> *javascript = f'''<script
> type="text/Javascript">IPython.notebook.kernel.execute("py_var = " +
> my_var)my_var = my_var + 1</script>'''HTML(javascript)*
>
>
> *#3:*
> *print(py_var)*
>
> To see this, run #1, followed by #2, and followed by #3. Rightly, the
> output of #3 would be *10*. If I run #2 and then #3 again, I would
> (rightly) get *11*.
>
> Then, if I run "#2 and below", the output of #3 is *still **11*, even
> though my_var actually held *12* and there was a pending 'py_var=12' in
> the execute queue.
>
> This is wrong. I really hope for the execute queue to be serviced before
> #3 runs ... not OK to single step through cells.
>
> Is there a way to get the execute queue serviced between cells even when
> running "#2 and below"?
>
> I have gone through kernel services code (
> https://github.com/jupyter/notebook/blob/master/notebook/static/services/kernels/kernel.js)
> and see how the execute() works from the client side. I think I'm really
> asking for explicit flushing of the shell channel at the server side ...
> not so obvious where to look for this.
>
> Is there any way to finesse this execute queue processing?
>
> Alternatively, is there a custom widget protocol that applies?
>
> Alternatively, should I be creating my own web socket and protocol for
> this??
>
> (I well understand how the shell protocol servicing strategy may not be
> suited to this, but I'm not sure where I should be going next.)
>
> Thanks!
>
> --
> You received this message because you are subscribed to the Google Groups
> "Project Jupyter" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jupyter/8bc7dcae-d6c9-409f-a3b5-457d9e815554o%40googlegroups.com
> <https://groups.google.com/d/msgid/jupyter/8bc7dcae-d6c9-409f-a3b5-457d9e815554o%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>


-- 
Travis DePrato (he/him/his)
Experiential Learning Software Architect
University of Michigan '19
Black Lives Matter

-- 
You received this message because you are subscribed to the Google Groups 
"Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jupyter/CANWNHrY0UV--Jb-wZnHzQX9acFGH4vEa5hXqzxrrC2Vab6jehg%40mail.gmail.com.

Reply via email to