{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Ensure notebooks can execute in parallel\n", "\n", "This notebook uses a file system based \"lock\" to assert that two instances of the notebook kernel will run in parallel. Each instance writes to a file in a temporary directory, and then tries to read the other file from\n", "the temporary directory, so that running them in sequence will fail, but running them in parallel will succed.\n", "\n", "Two notebooks are launched, each which sets the `this_notebook` variable. One notebook is set to `this_notebook = 'A'` and the other `this_notebook = 'B'`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import os.path\n", "import tempfile\n", "import time" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# the variable this_notebook is injectected in a cell above by the test framework.\n", "this_notebook = 'B'\n", "other_notebook = 'A'\n", "directory = os.environ['NBEXECUTE_TEST_PARALLEL_TMPDIR']\n", "with open(os.path.join(directory, 'test_file_{}.txt'.format(this_notebook)), 'w') as f:\n", " f.write('Hello from {}'.format(this_notebook))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "start = time.time()\n", "timeout = 5\n", "end = start + timeout\n", "target_file = os.path.join(directory, 'test_file_{}.txt'.format(other_notebook))\n", "while time.time() < end:\n", " time.sleep(0.1)\n", " if os.path.exists(target_file):\n", " with open(target_file, 'r') as f:\n", " text = f.read()\n", " if text == 'Hello from {}'.format(other_notebook):\n", " break\n", "else:\n", " assert False, \"Timed out – didn't get a message from {}\".format(other_notebook)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.7" } }, "nbformat": 4, "nbformat_minor": 2 }