{ "cells": [ { "cell_type": "markdown", "id": "7c946340-f845-4253-919f-313970f7ab82", "metadata": {}, "source": [ "# Threading python examples" ] }, { "cell_type": "markdown", "id": "86fcfc03-fe87-4e49-9e88-a28d34afa774", "metadata": {}, "source": [ "## Basic Usage" ] }, { "cell_type": "code", "execution_count": 15, "id": "19cc0bc6-910a-43a2-8b9a-ea626447b2bf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Finished in: 1.0109996795654297\n" ] } ], "source": [ "from threading import Thread\n", "from time import sleep, time\n", "\n", "def slowFunction(time=5):\n", " \"\"\"Create example of a slow function\"\"\"\n", " sleep(time)\n", "\n", "t0 = time()\n", "threads = []\n", "\n", "for i in range(10):\n", " t = Thread(target=slowFunction, args=(1,))\n", " t.start()\n", " threads.append(t)\n", "for t in threads:\n", " t.join(timeout=15)\n", "print(f\"Finished in: {time()-t0}\")\n" ] }, { "cell_type": "markdown", "id": "d27c1f20-9a89-4dbf-938e-b852c11aca45", "metadata": {}, "source": [ "## ThreadPoolExecutor" ] }, { "cell_type": "code", "execution_count": null, "id": "a16c284e-9735-4fd3-800a-1d04148dd84a", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.6" } }, "nbformat": 4, "nbformat_minor": 5 }