-
Notifications
You must be signed in to change notification settings - Fork 5
/
3.-event.py
60 lines (41 loc) · 1.03 KB
/
3.-event.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import time
import sys
sys.getrefcount
from itertools import cycle
import logging
from threading import Thread, Lock, Event
lock = Lock()
logging.basicConfig(
level=10,
format='%(threadName)s:%(levelname)s-%(message)s'
)
class BankFacilito:
def __init__(self):
self.balance = 0
def deposit(self):
for _ in range(1_000_000):
with lock:
self.balance += 1
def withdraw(self):
for _ in range(1_000_000):
with lock:
self.balance += 1
def loading(event):
for c in cycle('|/-|'):
if event.wait(.1):
break
print(c, end="\r", flush=True)
bank = BankFacilito()
start = time.time()
event = Event()
thread_a = Thread(target=bank.deposit)
thread_b = Thread(target=bank.withdraw)
thread_loading = Thread(target=loading, args=(event, ))
thread_a.start()
thread_b.start()
thread_loading.start()
thread_a.join()
thread_b.join()
event.set()
print("El programa demoro: ", time.time() - start)
print(bank.balance)