forked from ObjectProfile/PythonBridge
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtfactorial.py
More file actions
36 lines (30 loc) · 821 Bytes
/
tfactorial.py
File metadata and controls
36 lines (30 loc) · 821 Bytes
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
import argparse
import threading
import time
# Calculate the factorial
def factorial(n, t):
time.sleep(n*t/4)
print("Start: " + str(t) + ": " + str(n))
if n == 1:
res = 1
if t == 1:
print("Feel free to break here")
else:
res = n * factorial(n-1, t)
return res
# Calculate the factorial and print the result
def factorial_thread(n, t):
time.sleep(2)
result = factorial(n, t)
print("Thread " + str(t) + " = "+str(result))
def launch_factorials(n):
threads = []
print("Calculate: "+str(n))
breakpoint()
for i in range(n):
threads.append(threading.Thread(target=factorial_thread, args=(n+i, i+1)))
threads[-1].start()
print("Wait for the results")
for thread in threads:
thread.join()
print("Done")