python in 28

master
Bel LaPointe 2022-04-13 07:22:01 -06:00
parent a8edc611cd
commit 0bef5b997f
2 changed files with 31 additions and 0 deletions

31
src/python3.py Normal file
View File

@ -0,0 +1,31 @@
import threading
import queue
class T(threading.Thread):
def __init__(self, n, p, i, q):
super().__init__()
self.n = n
self.p = p
self.i = i
self.q = q
def run(self):
print(self.n, self.p, self.i, self.q)
start = self.i * ((self.n // self.p))
chunk = self.n // self.p + (0 if i < self.p-1 else self.n%self.p)
self.q.put(sum([j for j in range(start, start+chunk)]))
Ts = []
N = 100
P = 3
Q = queue.Queue()
for i in range(P):
Ts.append(T(N, P, i, Q))
for t in Ts:
t.start()
ttl = 0
for i in range(P):
ttl += Q.get()
for t in Ts:
t.join()
print(ttl)

View File