Skip to content

Commit 6d3d8b3

Browse files
committed
support delay for eventloop
1 parent 70aabb8 commit 6d3d8b3

11 files changed

Lines changed: 235 additions & 49 deletions

File tree

examples/eventloop/delay1.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import time
2+
from eventloop import EventLoop
3+
4+
run_time = 0
5+
6+
eventloop.set_debug(True)
7+
8+
9+
def test_func(arg1, arg2):
10+
global run_time
11+
run_time += 1
12+
print("Running test function with arguments:", arg1, arg2)
13+
return arg1 + arg2
14+
15+
16+
def test_func2(arg1, arg2):
17+
print("test function 2 with arguments:", arg1, arg2)
18+
return arg1 + arg2
19+
20+
21+
def test_callback(res):
22+
print("Running test callback function", res)
23+
assert res == "Hello World"
24+
25+
26+
# Test case 2: Add and run a periodic task
27+
28+
eventloop.start_new_task_once(
29+
test_func, ("Hello", " World"),
30+
callback=test_callback,
31+
delay_ms=2000
32+
)
33+
34+
eventloop.start_new_task(
35+
test_func2, ("Hello", " World"),
36+
is_periodic=True,
37+
period_ms=200,
38+
callback=test_callback
39+
)
40+
41+
# Sleep for enough time to allow the periodic task to run multiple times
42+
while run_time < 1:
43+
time.sleep(0.1)

package/eventloop/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# eventloop

package/eventloop/eventloop.py

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ class EventTask:
1414
_period_ms = None
1515
_is_periodic = False
1616
_last_call_time = 0
17+
_delay_ms = None
1718

18-
def __init__(self, func, callback, args, period_ms):
19+
def __init__(self, func, callback, args, period_ms, delay_ms):
1920
"""
2021
:param func: function to be called
2122
:param callback: callback function
@@ -26,6 +27,12 @@ def __init__(self, func, callback, args, period_ms):
2627
self._callback = callback
2728
self._args = args
2829
self._period_ms = period_ms
30+
self._delay_ms = delay_ms
31+
if not delay_ms is None:
32+
if period_ms is None:
33+
period_ms = 0
34+
self._last_call_time = time.tick_ms() - period_ms + delay_ms
35+
_debug('last_call_time for delay:', self._last_call_time)
2936
if period_ms != None:
3037
self._is_periodic = True
3138

@@ -49,7 +56,7 @@ def __init__(self, period_ms=100, thread_stack=0):
4956
self._period_ms = period_ms
5057
self._thread_stack = thread_stack
5158

52-
def _add_task(self, task_name, func, callback, args, period_ms):
59+
def _add_task(self, task_name, func, callback, args, period_ms, delay_ms):
5360
if task_name == None:
5461
self._uuid += 1
5562
task_name = str(self._uuid)
@@ -58,10 +65,18 @@ def _add_task(self, task_name, func, callback, args, period_ms):
5865
_debug('callback', callback)
5966
_debug('args', args)
6067
_debug('period_ms', period_ms)
61-
new_task = EventTask(func, callback, args, period_ms)
68+
_debug('delay_ms', delay_ms)
69+
new_task = EventTask(func, callback, args, period_ms, delay_ms)
6270
self._tasks[task_name] = new_task
6371

64-
def start_new_task(self, func, args, is_periodic=True, period_ms=1000, callback=None, task_name=None):
72+
def start_new_task(self,
73+
func, args,
74+
is_periodic=True,
75+
period_ms=1000,
76+
callback=None,
77+
task_name=None,
78+
delay_ms=None
79+
):
6580
"""
6681
Add a task to EventLoop
6782
:param task_name: name of task
@@ -71,21 +86,27 @@ def start_new_task(self, func, args, is_periodic=True, period_ms=1000, callback=
7186
:param args: arguments of func
7287
"""
7388
if is_periodic:
74-
self._add_task(task_name, func, callback, args, period_ms)
89+
self._add_task(task_name, func, callback,
90+
args, period_ms, delay_ms)
7591
else:
76-
self._add_task(task_name, func, callback, args, None)
92+
self._add_task(task_name, func, callback, args, None, delay_ms)
7793

78-
def start_new_task_once(self, func, args, callback=None, task_name=None):
94+
def start_new_task_once(self,
95+
func, args,
96+
callback=None,
97+
task_name=None,
98+
delay_ms=None):
7999
"""
80100
Add a task to EventLoop, run once
81101
:param task_name: name of task
82102
:param func: function to be called
83103
:param callback: callback function
84104
:param args: arguments of func
85105
"""
86-
self.start_new_task(func, args, False, None, callback, task_name)
106+
self.start_new_task(func, args, False, None,
107+
callback, task_name, delay_ms)
87108

88-
def start_new_task_periodic(self, func, args, period_ms=1000, callback=None, task_name=None):
109+
def start_new_task_periodic(self, func, args, period_ms=1000, callback=None, task_name=None, delay_ms=None):
89110
"""
90111
Add a task to EventLoop, run periodically
91112
:param task_name: name of task
@@ -94,7 +115,8 @@ def start_new_task_periodic(self, func, args, period_ms=1000, callback=None, tas
94115
:param callback: callback function
95116
:param args: arguments of func
96117
"""
97-
self.start_new_task(func, args, True, period_ms, callback, task_name)
118+
self.start_new_task(func, args, True, period_ms,
119+
callback, task_name, delay_ms)
98120

99121
def remove_task(self, task_name):
100122
"""
@@ -112,13 +134,14 @@ def _run_thread(self):
112134
while not self._need_stop:
113135
tick = time.tick_ms()
114136
for task_name, task in self._tasks.items():
115-
if task._is_periodic:
116-
if tick - task._last_call_time > task._period_ms:
117-
self._run_task(task)
118-
task._last_call_time = tick
119-
else:
137+
if tick - task._last_call_time > task._period_ms:
138+
_debug('run_task', task_name)
139+
_debug('tick', tick)
140+
_debug('last_call_time', task._last_call_time)
120141
self._run_task(task)
121-
self.remove_task(task_name)
142+
task._last_call_time = tick
143+
if not task._is_periodic:
144+
self.remove_task(task_name)
122145
if self._need_stop:
123146
break
124147
time.sleep_ms(self._period_ms)
@@ -171,7 +194,13 @@ def _get_default_event_loop():
171194
return g_default_event_loop
172195

173196

174-
def start_new_task(func, args, is_periodic=True, period_ms=1000, callback=None, task_name=None):
197+
def start_new_task(func, args,
198+
is_periodic=True,
199+
period_ms=1000,
200+
callback=None,
201+
task_name=None,
202+
delay_ms=None
203+
):
175204
"""
176205
Add a task to EventLoop
177206
:param task_name: name of task
@@ -182,10 +211,14 @@ def start_new_task(func, args, is_periodic=True, period_ms=1000, callback=None,
182211
"""
183212
eventloop = _get_default_event_loop()
184213
eventloop.start_new_task(func, args, is_periodic,
185-
period_ms, callback, task_name)
214+
period_ms, callback, task_name, delay_ms)
186215

187216

188-
def start_new_task_once(func, args, callback=None, task_name=None):
217+
def start_new_task_once(func, args,
218+
callback=None,
219+
task_name=None,
220+
delay_ms=None
221+
):
189222
"""
190223
Add a task to EventLoop, run once
191224
:param task_name: name of task
@@ -194,10 +227,15 @@ def start_new_task_once(func, args, callback=None, task_name=None):
194227
:param args: arguments of func
195228
"""
196229
eventloop = _get_default_event_loop()
197-
eventloop.start_new_task_once(func, args, callback, task_name)
230+
eventloop.start_new_task_once(func, args, callback, task_name, delay_ms)
198231

199232

200-
def start_new_task_periodic(func, args, period_ms=1000, callback=None, task_name=None):
233+
def start_new_task_periodic(func, args,
234+
period_ms=1000,
235+
callback=None,
236+
task_name=None,
237+
delay_ms=None
238+
):
201239
"""
202240
Add a task to EventLoop, run periodically
203241
:param task_name: name of task
@@ -208,7 +246,7 @@ def start_new_task_periodic(func, args, period_ms=1000, callback=None, task_name
208246
"""
209247
eventloop = _get_default_event_loop()
210248
eventloop.start_new_task_periodic(
211-
func, args, period_ms, callback, task_name)
249+
func, args, period_ms, callback, task_name, delay_ms)
212250

213251

214252
def remove_task(task_name):

port/linux/.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,8 @@
127127
],
128128
"[toml]": {
129129
"editor.defaultFormatter": "tamasfe.even-better-toml"
130+
},
131+
"[python]": {
132+
"editor.defaultFormatter": "ms-python.python"
130133
}
131134
}

0 commit comments

Comments
 (0)