Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
main
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
CDIS
Computer Sciences
Courses
CS544
s25
main
Commits
26a93cb3
Commit
26a93cb3
authored
4 weeks ago
by
TYLER CARAZA-HARTER
Browse files
Options
Downloads
Patches
Plain Diff
lec 2 demo
parent
bbd7c733
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lec/11-threads/code/lec2.ipynb
+173
-0
173 additions, 0 deletions
lec/11-threads/code/lec2.ipynb
with
173 additions
and
0 deletions
lec/11-threads/code/lec2.ipynb
0 → 100644
+
173
−
0
View file @
26a93cb3
{
"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
}
%% Cell type:code id:de014d74-4685-4f16-85fd-f571fd5a91db tags:
```
python
import
threading
```
%% Cell type:code id:af3da760-5f6e-40c7-94cd-b505abe59012 tags:
```
python
def
task
():
print
(
"
hello from thread ID:
"
,
threading
.
get_native_id
())
#task()
t
=
threading
.
Thread
(
target
=
task
)
t
.
start
()
print
(
"
hello from main thread, with ID:
"
,
threading
.
get_native_id
())
```
%% Output
hello from thread ID:hello from main thread, with ID: 589
602
%% Cell type:code id:3dd19fcb-2c4a-4b26-b72f-3b87bd8e5bb5 tags:
```
python
total
=
0
def
task
(
count
):
global
total
for
i
in
range
(
count
):
total
+=
1
t
=
threading
.
Thread
(
target
=
task
,
args
=
[
1_000_000
])
t
.
start
()
t
.
join
()
# wait until the thread is done before we continue
total
```
%% Output
1000000
%% Cell type:code id:de6ce736-5bdf-42b9-b98c-afee738b8f94 tags:
```
python
total
```
%% Output
1000000
%% Cell type:code id:b735264e-f27a-4d27-ba5f-4d6a1f53543e tags:
```
python
total
=
0
def
task
(
count
):
global
total
for
i
in
range
(
count
):
total
+=
1
t1
=
threading
.
Thread
(
target
=
task
,
args
=
[
1_000_000
])
t1
.
start
()
t2
=
threading
.
Thread
(
target
=
task
,
args
=
[
1_000_000
])
t2
.
start
()
t1
.
join
()
t2
.
join
()
total
```
%% Output
1100428
%% Cell type:code id:c3d4a396-1793-4504-b2ff-fb8b81ae48d5 tags:
```
python
import
dis
dis
.
dis
(
"
total += 1
"
)
```
%% Output
0 RESUME 0
1 LOAD_NAME 0 (total)
LOAD_CONST 0 (1)
BINARY_OP 13 (+=)
STORE_NAME 0 (total)
RETURN_CONST 1 (None)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment