# Using cesium.js within Jupyter, web workers do not terminate, leaking resources

**URL:** <https://community.cesium.com/t/using-cesium-js-within-jupyter-web-workers-do-not-terminate-leaking-resources/8613>\
**Category:** CesiumJS\
**Created:** [September 5, 2019, 3:25am UTC](https://community.cesium.com/t/using-cesium-js-within-jupyter-web-workers-do-not-terminate-leaking-resources/8613 "2019-09-05T03:25:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marc\_Allen](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/marc_allen/32/3553_2.png) [@Marc\_Allen](https://community.cesium.com/u/Marc_Allen)\
**Post date:** [September 5, 2019, 3:25am UTC](https://community.cesium.com/t/using-cesium-js-within-jupyter-web-workers-do-not-terminate-leaking-resources/8613/1 "2019-09-05T03:25:05Z")

</div>

**1. A concise explanation of the problem you’re experiencing.**

I am trying to use cesium.js within a Jupyter notebook, where I am emitting HTML to render a Viewer within a step in the notebook. When I am iterating in the notebook, running the “step” that renders the Viewer repeatedly, eventually the browser (both Chrome and Firefox) gets laggy and FPS drops.

I noticed that the number of web worker thread instances increases monotonically upon each step execution, and they don’t seem to terminate after the Jupyter step has re-run. In some cases, I am loading large CZML files with GLTF models, so memory/CPU consumption quickly becomes an issue.

**2. A minimal code example. If you’ve found a bug, this helps us reproduce and repair it.**

In a jupyter notebook, enter this content into a cell and execute it ten times. In the Developer Tools, you’ll see ten web workers, which are labeled with GUIDs.

---

<div class="post-metadata">

**Author:** ![Marc\_Allen](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/marc_allen/32/3553_2.png) [@Marc\_Allen](https://community.cesium.com/u/Marc_Allen)\
**Post date:** [September 6, 2019, 4:47pm UTC](https://community.cesium.com/t/using-cesium-js-within-jupyter-web-workers-do-not-terminate-leaking-resources/8613/2 "2019-09-06T16:47:10Z")

</div>

Update: I was able to work around this by wrapping the HTML in an iframe and using the srcdoc and sandbox allow-scripts attributes. This appears to trigger the correct behavior for cleaning up the web worker resources.

---

<div class="post-metadata">

**Author:** ![Timothy\_Dunn](https://avatars.discourse-cdn.com/v4/letter/t/f05b48/32.png) [@Timothy\_Dunn](https://community.cesium.com/u/Timothy_Dunn)\
**Post date:** [September 6, 2019, 10:05pm UTC](https://community.cesium.com/t/using-cesium-js-within-jupyter-web-workers-do-not-terminate-leaking-resources/8613/3 "2019-09-06T22:05:59Z")

</div>

Can you please be slightly more specific on how you managed to get it to work in a Notebook? I tried a few different ways last year so I could do some workshops on using CesiumJS. Sadly I resorted to just using the Sandbox straight but it was insanely confusing to the participants. I/we normally always use Notebooks as they are so much better for trainings so if you managed to get a working solution I would kill to chat with you on it if you don’t mind.

---

<div class="post-metadata">

**Author:** ![Marc\_Allen](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/marc_allen/32/3553_2.png) [@Marc\_Allen](https://community.cesium.com/u/Marc_Allen)\
**Post date:** [September 6, 2019, 11:19pm UTC](https://community.cesium.com/t/using-cesium-js-within-jupyter-web-workers-do-not-terminate-leaking-resources/8613/4 "2019-09-06T23:19:15Z")

</div>

I’m using JupyterLab 1.0, running inside a docker container for development. Try this as a hello world, running it inside one step. It’s a bit hacky, admittedly.

> def cesium\_html():

> ```
> return \
> 
> ```

> ```
> """
> 
> ```

> ```
> <iframe style="width: 1024px; height: 768px; overflow:hidden; border: 0px; margin: 0px" scrolling="no"
> 
> ```

> ```
> frameborder="0" marginheight="0" marginwidth="0" sandbox="allow-scripts" srcdoc='
> 
> ```

> 

> ```
> <!DOCTYPE html>
> 
> ```

> ```
> <html lang="en">
> 
> ```

> ```
> <head>
> 
> ```

> ```
> <meta charset="utf-8">
> 
> ```

> ```
> </head>
> 
> ```

> ```
> <body>
> 
> ```

> ```
> <script src="https://cesiumjs.org/releases/1.60/Build/Cesium/Cesium.js"></script>
> 
> ```

> ```
> <link href="https://cesiumjs.org/releases/1.60/Build/Cesium/Widgets/widgets.css" rel="stylesheet">
> 
> ```

> ```
> <div id="cesiumContainer" style="width: 1024px; height:768px"></div>
> 
> ```

> ```
> <script>
> 
> ```

> ```
> //Cesium.IondefaultAccessToken = "your_access_token";
> 
> ```

> ```
> var viewer = new Cesium.Viewer("cesiumContainer");
> 
> ```

> ```
> </script>
> 
> ```

> ```
> </body>
> 
> ```

> ```
> </html>
> 
> ```

> 

> ```
> '></iframe>
> 
> ```

> ```
> """
> 
> ```

> 

> import IPython.display

> IPython.display.HTML(cesium\_html())

You should see a cesium globe appear. The iframe was necessary to avoid leaking the web worker threads, which will eventually cripple your computer 🙂

In use case, I’m using CZML, with the czml3 python library, to create and render my data inside cesium, though I’ll probably switch to native javascript soon as I need more flexibility.

I’m not a Jupyter expert, so there is probably a better way. I expect that the right way to do this is a formal “widget”, something like [https://github.com/petrushy/CesiumWidget/blob/master/CesiumWidget/cesiumwidget.py](https://github.com/petrushy/CesiumWidget/blob/master/CesiumWidget/cesiumwidget.py), but it’s a bit out of date and I haven’t tried it. There’s also [https://github.com/sinhrks/cesiumpy/tree/master/cesiumpy](https://github.com/sinhrks/cesiumpy/tree/master/cesiumpy) but it also is a bit neglected and didn’t work for me, so I ended up doing my own thing.
