# Script Edge Cases

**URL:** <https://forum.crosscompute.com/t/script-edge-cases/239>\
**Category:** Discussions\
**Created:** [May 22, 2025, 4:07pm UTC](https://forum.crosscompute.com/t/script-edge-cases/239 "2025-05-22T16:07:21Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![invisibleroads](https://forum.crosscompute.com/user_avatar/forum.crosscompute.com/invisibleroads/32/3_2.png) [@invisibleroads](https://forum.crosscompute.com/u/invisibleroads)\
**Post date:** [May 22, 2025, 4:07pm UTC](https://forum.crosscompute.com/t/script-edge-cases/239/1 "2025-05-22T16:07:21Z")

</div>

For a few hours, I debated whether to replace `await` expressions with `asyncio.run()` calls when converting Jupyter notebooks into Python scripts.

I experimented a bit with the following regular expression, which can detect occurrences of the `await` keyword that are not enclosed in single or double quotes (thanks to [https://stackoverflow.com/a/23667311/192092](https://stackoverflow.com/a/23667311/192092)). You can combine this pattern with `AWAIT_PATTERN.sub(f, text)` where the match is positive if `match.group(1) != None` for `f(match)`.

```python
AWAIT_PATTERN = re.compile(r'\'[^\']+\'|"[^"]+"|(\bawait\b)')

```

Ultimately, I decided against it, as a solution involving regular expressions will not cover cases such as `async with` and `async for`.

I also debated whether to wrap the notebook code inside an `async` function if `await` is detected.

After reading [an article that says every Jupyter Notebook cell runs in an async loop](https://daniel.feldroy.com/posts/til-2025-01-every-jupyter-notebook-cell-runs-in-an-async-loop), I decided to use the wrapper solution. So CrossCompute should be able to support most if not all cases of using `await` and other asynchronous techniques in Jupyter notebooks, as long as the code runs without error in Jupyter.
