From 26a93cb38077810d17dfb701c58eebbf2515fe25 Mon Sep 17 00:00:00 2001 From: TYLER CARAZA-HARTER <tharter@cs544-tharter.cs.wisc.edu> Date: Fri, 14 Feb 2025 13:55:06 -0600 Subject: [PATCH] lec 2 demo --- lec/11-threads/code/lec2.ipynb | 173 +++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 lec/11-threads/code/lec2.ipynb diff --git a/lec/11-threads/code/lec2.ipynb b/lec/11-threads/code/lec2.ipynb new file mode 100644 index 0000000..1abbbac --- /dev/null +++ b/lec/11-threads/code/lec2.ipynb @@ -0,0 +1,173 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "de014d74-4685-4f16-85fd-f571fd5a91db", + "metadata": {}, + "outputs": [], + "source": [ + "import threading" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "af3da760-5f6e-40c7-94cd-b505abe59012", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello from thread ID:hello from main thread, with ID: 589\n", + " 602\n" + ] + } + ], + "source": [ + "def task():\n", + " print(\"hello from thread ID:\", threading.get_native_id())\n", + "\n", + "#task()\n", + "t = threading.Thread(target=task)\n", + "t.start()\n", + "print(\"hello from main thread, with ID:\", threading.get_native_id())" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "3dd19fcb-2c4a-4b26-b72f-3b87bd8e5bb5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1000000" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "total = 0\n", + "\n", + "def task(count):\n", + " global total\n", + " for i in range(count):\n", + " total += 1\n", + "\n", + "t = threading.Thread(target=task, args=[1_000_000])\n", + "t.start()\n", + "t.join() # wait until the thread is done before we continue\n", + "total" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "de6ce736-5bdf-42b9-b98c-afee738b8f94", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1000000" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "total" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "b735264e-f27a-4d27-ba5f-4d6a1f53543e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1100428" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "total = 0\n", + "\n", + "def task(count):\n", + " global total\n", + " for i in range(count):\n", + " total += 1\n", + "\n", + "t1 = threading.Thread(target=task, args=[1_000_000])\n", + "t1.start()\n", + "t2 = threading.Thread(target=task, args=[1_000_000])\n", + "t2.start()\n", + "\n", + "t1.join()\n", + "t2.join()\n", + "\n", + "total" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "c3d4a396-1793-4504-b2ff-fb8b81ae48d5", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " 0 RESUME 0\n", + "\n", + " 1 LOAD_NAME 0 (total)\n", + " LOAD_CONST 0 (1)\n", + " BINARY_OP 13 (+=)\n", + " STORE_NAME 0 (total)\n", + " RETURN_CONST 1 (None)\n" + ] + } + ], + "source": [ + "import dis\n", + "dis.dis(\"total += 1\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3.13-nogil", + "language": "python", + "name": "python3.13-nogil" + }, + "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.13.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} -- GitLab