-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcore.py
79 lines (63 loc) · 1.83 KB
/
core.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
69
70
71
72
73
74
75
76
77
78
79
# This file is part of MaixUI
# Copyright (c) sipeed.com
#
# Licensed under the MIT license:
# http://www.opensource.org/licenses/mit-license.php
#
class agent:
def __init__(self):
self.msg = []
self.arg = {}
import time
self.get_ms = (lambda : time.ticks_ms()) if getattr(time, "ticks_ms", False) else (lambda : time.time() * 1000)
def event(self, cycle, func, args=None):
# arrived, cycle, function
tmp = (self.get_ms() + cycle, cycle, func, args)
#print('self.event', tmp)
self.msg.append(tmp)
def remove(self, func):
#print(self.remove)
for pos in range(len(self.msg)): # maybe use map
#print(self.msg[pos][2], func)
if self.msg[pos][2] == func:
self.msg.remove(self.msg[pos])
break
#print(self.msg)
def call(self, obj, pos=0):
#print(self.call, pos, obj)
self.msg.pop(pos)
self.event(obj[1], obj[2], obj[3])
obj[2](obj[3]) if obj[3] else obj[2]() # exec function
def cycle(self):
if (len(self.msg)):
obj = self.msg[0]
if (self.get_ms() >= obj[0]):
self.call(obj, 0)
def parallel_cycle(self):
for pos in range(len(self.msg)): # maybe use map
obj = self.msg[pos]
if (self.get_ms() >= obj[0]):
self.call(obj, pos)
break
def unit_test(self):
class tmp:
def test_0(self):
print('test_0')
def test_1():
print('test_1')
def test_2(self):
print('test_2')
self.remove(tmp.test_1)
self.event(1000, tmp.test_1)
self.remove(tmp.test_2)
import time
self.event(200, tmp.test_0, self)
self.event(10, tmp.test_1)
self.event(2000, tmp.test_2, self)
while True:
self.parallel_cycle()
time.sleep(0.1)
system = agent()
if __name__ == "__main__":
#agent().unit_test()
system.unit_test()