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

lec 2 demo

parent bbd7c733
No related branches found
No related tags found
No related merge requests found
%% 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)
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