Skip to content

ZMQInteractiveShell does not run post_execute and post_run_cell events on async cells #520

@JoaoFelipe

Description

@JoaoFelipe

In the notebook repository, I submitted an issue indicating that some cells did not display matplotlib images and delayed their display to the next cell (jupyter/notebook#5568). After some inspection, I noticed that it only occurs in Python >= 3.8 and that the code elements that cause the issue are comprehensions, which are being detected by IPython as async cells. So, I submitted another issue to the IPython repository related to the misdetection (ipython/ipython#12422).

However, async cells should not prevent matplotlib from displaying in the right cells after the end of their executions. I inspected it further and realized that the problem with async cells is that they do not run post_execute and post_run_cell events.

Here is a small example with the bug that does not use comprehensions nor matplotib (so it is easier to create a test and if someone fixes the async detection in the IPython repository, it will not impact this bug report):

In [1]:

COUNTER = 0
def count():
    global COUNTER
    COUNTER += 1
    print("Count", COUNTER)
ip = get_ipython()
ip.events.register('post_execute', count)
print('A')

A
Count 1

In [2]:

import asyncio
await asyncio.sleep(0)
print('B')

B

In [3]:

print('C')

C
Count 2


I'm pretty sure this is the main cause of the bug that I reported in the notebook repository (jupyter/notebook#5568). Should I close the issue there now or wait for a fix of the bug here?

This issue is also related to #514

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions