@@ -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
214252def remove_task (task_name ):
0 commit comments