-
Notifications
You must be signed in to change notification settings - Fork 7
/
timer.py
63 lines (50 loc) · 1.48 KB
/
timer.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
##
# timer.py
# Demo TIBRV Timer Callback
#
# LAST MODIFIED: V1.0 2016-12-22 ARIEN arien.chen@gmail.com
#
import sys
import time
from pytibrv.api import *
from pytibrv.status import *
from pytibrv.events import *
from pytibrv.msg import *
from pytibrv.disp import *
from pytibrv.queue import *
from datetime import datetime
def callback(event:tibrvEvent, msg: tibrvMsg, closure):
print('HI', datetime.now())
# MAIN PROGRAM
def main(argv):
status = tibrv_Open()
assert TIBRV_OK == status, tibrvStatus_GetText(status)
# DEFAULT QUE
que = TIBRV_DEFAULT_QUEUE
# create timer
status, event = tibrvEvent_CreateTimer(que, callback, 0.5)
assert TIBRV_OK == status, tibrvStatus_GetText(status)
# use dispatcher thread
print('create dispacher thread')
status, disp = tibrvDispatcher_Create(que, TIBRV_WAIT_FOREVER)
# run for 5 sec
timeout = time.time() + 5
cnt = 0
while time.time() <= timeout:
cnt += 1
print(cnt, 'WAITING ...')
time.sleep(1.0)
status = tibrvDispatcher_Destroy(disp)
assert TIBRV_OK == status, tibrvStatus_GetText(status)
# dispatch in main(current) thread
# run for 5 sec
print('run in main thread')
timeout = time.time() + 5
while time.time() <= timeout:
tibrvQueue_TimedDispatch(que, 1.0)
status = tibrvEvent_Destroy(event)
assert TIBRV_OK == status, tibrvStatus_GetText(status)
tibrv_Close()
print('end')
if __name__ == "__main__":
main(sys.argv)