jupyter / jupyter/notebook

Notebook hangs when repeatedly calling Python extension module function

Open
#5,232 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Jupyter Notebook
Stars
13.3k
Forks
5.8k
Avg merge
6d 11h
Merged PRs (30d)
7

Description

This code runs fine in command-line ipython, but breaks Jupyter notebook (same virtual env):

```python
import numpy as np

import cdyn

c = 0.251 + .0001j
maxiter = 10000
res = 1000
extent = (.32, .62, -.27, .03)

x = np.linspace(extent[0], extent[1], num=res+1)
y = np.linspace(extent[2], extent[3], num=res+1)

for i in range(res+1):
print(i) # added for debugging
for j in range(res+1):
cdyn.test_fulljulia(x[i], y[j], c, maxiter)
```

I call a C function from a custom extension module 1,000,000 times (once in each iteration of a double loop of 1000 iterations each). For the purposes of debugging, I'm ignoring the return value of `cdyn.test_fulljulia`, but the original script that reads those values successfully plots a Julia set image.

When I run the code above in command-line ipython, it always works fine and takes around 30 seconds. However, when I run the exact same code in Jupyter notebook, the kernel freezes, and the only way to get rid of the `[*]` status indicator is to shutdown the kernel (pressing `ii` to send an interrupt signal does not work), close the notebook tab in Firefox, and open it again.

I don't think the C function is the culprit. It is actually very simple, just math, no memory allocation at all (except for a `return PyLong_FromLong(i);` at the end). It's in this gist https://gist.github.com/fofoni/cc9fda75298f2394a17ef42f5e9b7935 if anyone wants to take a look.

When I add the `print(i)` line as in the code above, I can see that multiple iterations of the loop are successfully run before it freezes. The number of times the kernel calls `cdyn.test_fulljulia` looks random to me (each time I close/reopen the notebook and try again, it's a different number), but usually 10,000 to 100,000 times (that is, the last `print(i)` I see is between 10 and 100). Indeed, if I tune down that `res`olution parameter, then the Jupyter notebook that plots the Julia set works just fine.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.