-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainFuncs.py
68 lines (65 loc) · 1.97 KB
/
mainFuncs.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
61
62
63
64
65
66
67
68
from GlvCommon import *
from math import *
from time import sleep, time
import multiprocessing
class Sinusoidal(Func):
def __init__(self, amp: float, period: float, **kwargs):
super().__init__()
self.amp = amp
self.phase = 2 * pi / period
def shouldUpdate(self):
return True
def update(self):
self.out(sin(time() * self.phase) * self.amp)
class AddN(Func):
def __init__(self, opOne: Output[int], n, **kwargs):
super().__init__()
self.n = n
def update(self):
if self.opOne() is not None:
newVal = self.opOne() + self.n
self.out(newVal)
sleep(1)
class Counter(Func):
def __init__(self, src: Output[int], **kwargs):
super().__init__()
self.counter = 0
def update(self):
if self.src():
self.counter = self.counter + 1
self.out(self.counter)
def fib(n):
a = 0
b = 1
if n < 0:
print("Incorrect input")
elif n == 0:
return a
elif n == 1:
return b
else:
for i in range(2,n):
c = a + b
a = b
b = c
return b
class Fibbonaci(Func):
def __init__(self, **kwargs):
super().__init__()
pool()
self.ready = True
def shouldUpdate(self):
return self.ready
def update(self):
self.ready = False
result = pool().apply_async(fib, (90000,))
start = time()
try:
# a high timeout may lead to slower shutdowns as the thread can't check for its exit condition
val = result.get(timeout=3)
#self.out(val) # remove comment if you want to dump the actual value of the fib
except multiprocessing.TimeoutError:
print("Process timed out")
span = time() - start
self.out(span) # comment out if you want the value of the fib. This just shows how long it takes to calculate the fib
self.ready = True