Skip to content
Snippets Groups Projects
Commit bbd7c733 authored by TYLER CARAZA-HARTER's avatar TYLER CARAZA-HARTER
Browse files

lec 1 demo

parent 651ca94e
No related branches found
No related tags found
No related merge requests found
%% 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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment