Skip to content

Commit 521b83d

Browse files
committed
Add tfactorial.py test file
1 parent 8cabdaa commit 521b83d

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

tfactorial.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import argparse
2+
import threading
3+
import time
4+
5+
6+
# Calculate the factorial
7+
def factorial(n, t):
8+
time.sleep(n*t/4)
9+
print("Start: " + str(t) + ": " + str(n))
10+
if n == 1:
11+
res = 1
12+
if t == 1:
13+
print("Feel free to break here")
14+
else:
15+
res = n * factorial(n-1, t)
16+
return res
17+
18+
19+
# Calculate the factorial and print the result
20+
def factorial_thread(n, t):
21+
time.sleep(2)
22+
result = factorial(n, t)
23+
print("Thread " + str(t) + " = "+str(result))
24+
25+
26+
def launch_factorials(n):
27+
threads = []
28+
print("Calculate: "+str(n))
29+
breakpoint()
30+
for i in range(n):
31+
threads.append(threading.Thread(target=factorial_thread, args=(n+i, i+1)))
32+
threads[-1].start()
33+
print("Wait for the results")
34+
for thread in threads:
35+
thread.join()
36+
print("Done")

0 commit comments

Comments
 (0)