{ "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": "82b2f08c-1a13-4a0f-9546-e5d624dbee5b", "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\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "ae617ed8-dd51-4154-ad83-308df527d1f1", "metadata": {}, "outputs": [], "source": [ "import threading" ] }, { "cell_type": "code", "execution_count": 4, "id": "b735264e-f27a-4d27-ba5f-4d6a1f53543e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 151 ms, sys: 0 ns, total: 151 ms\n", "Wall time: 148 ms\n" ] }, { "data": { "text/plain": [ "2000000" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "# 141 ms (no locks)\n", "# 340 ms (fine-grained locking)\n", "# 122 ms (coarse-grained locking)\n", "\n", "lock = threading.Lock() # this protects total\n", "total = 0\n", "\n", "def task(count):\n", " global total\n", " lock.acquire()\n", " for i in range(count):\n", " total += 1\n", " lock.release()\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": 5, "id": "7cf83d46-b7db-4ab6-b0bb-4b35fecba5b2", "metadata": {}, "outputs": [], "source": [ "bank_accounts = {\"x\": 25, \"y\": 100, \"z\": 200} # in dollars\n", "lock = threading.Lock() # protects bank_accounts\n", "\n", "def transfer(src, dst, amount):\n", " with lock: # automatically acquire now, automatically release after the with\n", " success = False\n", " if bank_accounts[src] >= amount:\n", " bank_accounts[src] -= amount\n", " bank_accounts[dst] += amount\n", " success = True\n", " print(\"transferred\" if success else \"denied\")\n", " print(\"is it locked inside the with?\", lock.locked())\n", " print(\"is it locked after the with?\", lock.locked())" ] }, { "cell_type": "code", "execution_count": 6, "id": "c9f19a3d-172f-4103-b631-11b42a8dc94c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "transferred\n", "is it locked inside the with? True\n", "is it locked after the with? False\n" ] }, { "data": { "text/plain": [ "{'x': 5, 'y': 120, 'z': 200}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "transfer(\"x\", \"y\", 20)\n", "bank_accounts" ] }, { "cell_type": "code", "execution_count": 7, "id": "bdd94c73-dd01-4b9e-aecc-70faff00685d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "denied\n", "is it locked inside the with? True\n", "is it locked after the with? False\n" ] }, { "data": { "text/plain": [ "{'x': 5, 'y': 120, 'z': 200}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "transfer(\"x\", \"z\", 10)\n", "bank_accounts" ] }, { "cell_type": "code", "execution_count": 8, "id": "41f9d7d2-86f8-433c-bf56-724a7f445753", "metadata": {}, "outputs": [ { "ename": "KeyError", "evalue": "'w'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[8], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mtransfer\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mw\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mz\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# there is no \"w\" bank account\u001b[39;00m\n\u001b[1;32m 2\u001b[0m bank_accounts\n", "Cell \u001b[0;32mIn[5], line 7\u001b[0m, in \u001b[0;36mtransfer\u001b[0;34m(src, dst, amount)\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m lock: \u001b[38;5;66;03m# automatically acquire now, automatically release after the with\u001b[39;00m\n\u001b[1;32m 6\u001b[0m success \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m\n\u001b[0;32m----> 7\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[43mbank_accounts\u001b[49m\u001b[43m[\u001b[49m\u001b[43msrc\u001b[49m\u001b[43m]\u001b[49m \u001b[38;5;241m>\u001b[39m\u001b[38;5;241m=\u001b[39m amount:\n\u001b[1;32m 8\u001b[0m bank_accounts[src] \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m=\u001b[39m amount\n\u001b[1;32m 9\u001b[0m bank_accounts[dst] \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m amount\n", "\u001b[0;31mKeyError\u001b[0m: 'w'" ] } ], "source": [ "transfer(\"w\", \"z\", 10) # there is no \"w\" bank account\n", "bank_accounts" ] }, { "cell_type": "code", "execution_count": 9, "id": "f193975b-6f75-4c3c-b789-c494ad1130a6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "transferred\n", "is it locked inside the with? True\n", "is it locked after the with? False\n" ] }, { "data": { "text/plain": [ "{'x': 5, 'y': 170, 'z': 150}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "transfer(\"z\", \"y\", 50)\n", "bank_accounts" ] }, { "cell_type": "code", "execution_count": null, "id": "cd9dd92a-3d2c-47c7-85f1-4bec952d1a67", "metadata": {}, "outputs": [], "source": [] } ], "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 }