forked from dunossauro/live-de-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03.py
More file actions
62 lines (43 loc) · 1.12 KB
/
Copy path03.py
File metadata and controls
62 lines (43 loc) · 1.12 KB
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
61
62
from threading import Thread, currentThread
from queue import Queue
from time import sleep
from functools import reduce
q = Queue(maxsize=2)
matriz = [[2, 9],
[-1, 6]]
def principal(mat):
sleep(10)
q.put(mat[0][0] * mat[1][1])
def secundaria(mat):
count = 0
while q.empty():
count += 1
print('secundaria', count)
q.put(mat[1][0] * mat[0][1])
def result():
count = 0
while not q.full():
count += 1
print('result', count)
return reduce(lambda x, y: x - y, q.queue)
t_p = Thread(target=principal, kwargs={'mat': matriz}, name='Principal')
t_p.start()
t_s = Thread(target=secundaria, kwargs={'mat': matriz}, name='Secundaria')
t_s.start()
class Result(Thread):
def __init__(self, target):
super().__init__()
self.result = None
self.target = target
self.stoped = False
def start(self):
self.result = self.target()
self.stoped = True
def join(self):
while not self.stoped:
sleep(0.1)
return self.result
t_r = Result(target=result)
print(currentThread())
t_r.start()
print(t_r.join())