mirror of
https://github.com/autistic-symposium/web3-starter-py.git
synced 2025-05-17 22:20:22 -04:00
add thread example
This commit is contained in:
parent
90049c80af
commit
28e68f4b30
1 changed files with 20 additions and 0 deletions
20
Concurrence_examples/unsafe_thread_example.py
Normal file
20
Concurrence_examples/unsafe_thread_example.py
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
import time
|
||||||
|
import threading
|
||||||
|
|
||||||
|
counter = 0
|
||||||
|
threads = []
|
||||||
|
|
||||||
|
def count():
|
||||||
|
global counter
|
||||||
|
for _ in range(100):
|
||||||
|
counter += 1
|
||||||
|
|
||||||
|
for _ in range(100):
|
||||||
|
thread = threading.Thread(target=count)
|
||||||
|
thread.start()
|
||||||
|
threads.append(thread)
|
||||||
|
|
||||||
|
for thread in threads:
|
||||||
|
thread.join()
|
||||||
|
|
||||||
|
print(f"Count: {counter}")
|
Loading…
Add table
Add a link
Reference in a new issue