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
bbd7c733
Commit
bbd7c733
authored
4 weeks ago
by
TYLER CARAZA-HARTER
Browse files
Options
Downloads
Patches
Plain Diff
lec 1 demo
parent
651ca94e
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/lec1.ipynb
+163
-0
163 additions, 0 deletions
lec/11-threads/code/lec1.ipynb
with
163 additions
and
0 deletions
lec/11-threads/code/lec1.ipynb
0 → 100644
+
163
−
0
View file @
bbd7c733
{
"cells": [
{
"cell_type": "code",
"execution_count": 14,
"id": "3b8d1ed9-9568-473e-bfa2-3b08f9e4587f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hi from thread ID:hi from main thread, ID: 65\n",
" 125\n"
]
}
],
"source": [
"import threading\n",
"import time\n",
"\n",
"def task():\n",
" print(\"hi from thread ID:\", threading.get_native_id())\n",
"\n",
"t = threading.Thread(target=task)\n",
"t.start()\n",
"print(\"hi from main thread, ID:\", threading.get_native_id())"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "651dc205-4fdc-4054-be28-b19e2e798017",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1000000\n"
]
}
],
"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 it exits\n",
"print(total)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "5eb4a3e1-decf-4352-b61c-b10e53aec763",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1000"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"total"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "ceeb7904-ff2c-424c-bf7c-724d1b285e3b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1084635"
]
},
"execution_count": 33,
"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",
"\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": 36,
"id": "48a5226c-6d63-4062-a2be-f7dd890c4dc1",
"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:3b8d1ed9-9568-473e-bfa2-3b08f9e4587f tags:
```
python
import
threading
import
time
def
task
():
print
(
"
hi from thread ID:
"
,
threading
.
get_native_id
())
t
=
threading
.
Thread
(
target
=
task
)
t
.
start
()
print
(
"
hi from main thread, ID:
"
,
threading
.
get_native_id
())
```
%% Output
hi from thread ID:hi from main thread, ID: 65
125
%% Cell type:code id:651dc205-4fdc-4054-be28-b19e2e798017 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 it exits
print
(
total
)
```
%% Output
1000000
%% Cell type:code id:5eb4a3e1-decf-4352-b61c-b10e53aec763 tags:
```
python
total
```
%% Output
1000
%% Cell type:code id:ceeb7904-ff2c-424c-bf7c-724d1b285e3b 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
1084635
%% Cell type:code id:48a5226c-6d63-4062-a2be-f7dd890c4dc1 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