MaxPlus Python API Reference
Python3/pymxs/timer.py
1 """
2  Demonstrate timers.
3 """
4 import threading
5 import time
6 import pymxs # pylint: disable=import-error
7 
8 GLOBAL_ARGS = {
9  "nativeCallTagA": 1,
10  "nativeCallTagB": "",
11  "nativeCallWithWrapperObjTagA": 1,
12  "nativeCallWithWrapperObjName": u"TestName",
13  "wrapObj": None
14 }
15 
16 def native_call(first, second, local_env):
17  """Run timer payload"""
18  local_env["nativeCallTagA"] = first
19  local_env["nativeCallTagB"] = second
20 
21 def native_with_wrapper(first, wrap_obj, local_env):
22  """Run timer payload"""
23  local_env["nativeCallWithWrapperObjTagA"] = first
24  wrap_obj.Name = local_env["nativeCallWithWrapperObjName"]
25  local_env["wrapObj"] = wrap_obj
26 
27 def check(local_env):
28  """Validate expected results"""
29  is_success = True
30  if local_env["nativeCallTagA"] != 10 or local_env["nativeCallTagB"] != "second":
31  is_success = False
32  print("Error: Incorrect native call for threading timer")
33 
34  wrap_teapot = local_env["wrapObj"]
35  if (local_env["nativeCallWithWrapperObjTagA"] != 10 or
36  wrap_teapot is None or
37  wrap_teapot.Name != local_env["nativeCallWithWrapperObjName"]):
38  is_success = False
39  print("Error: Incorrect native call with wrapper object for threading timer")
40 
41  return is_success
42 
43 def main():
44  """Demonstrate timers"""
45  wrap_teapot = pymxs.runtime.Teapot()
46 
47  # test native call
48  native_call_timer = threading.Timer(
49  0.1,
50  native_call,
51  [10, "second"],
52  kwargs={"local_env": GLOBAL_ARGS})
53  native_with_wrapper_timer = threading.Timer(
54  0.1,
55  native_with_wrapper,
56  kwargs={"first": 10, "wrap_obj": wrap_teapot, "local_env": GLOBAL_ARGS})
57  print("Start timer")
58  native_call_timer.start()
59  native_with_wrapper_timer.start()
60  time.sleep(0.2)
61  if check(GLOBAL_ARGS):
62  print("threading timer success")
63 
64 main()