demoTimer.py

demoTimer.py
1 import pymxs
2 import threading
3 import time
4 
5 globalArgs = {
6  "nativeCallTagA": 1,
7  "nativeCallTagB": "",
8  "nativeCallWithWrapperObjTagA": 1,
9  "nativeCallWithWrapperObjName": u"TestName",
10  "wrapObj": None
11 }
12 
13 
14 def nativeCall(a, b, localEnv):
15  localEnv["nativeCallTagA"] = a
16  localEnv["nativeCallTagB"] = b
17 
18 
19 def nativeWithWrapper(a, wrapObj, localEnv):
20  localEnv["nativeCallWithWrapperObjTagA"] = a
21  wrapObj.Name = localEnv["nativeCallWithWrapperObjName"]
22  localEnv["wrapObj"] = wrapObj
23 
24 
25 def check(localEnv):
26  isSuccess = True
27  if localEnv["nativeCallTagA"] != 10 or localEnv["nativeCallTagB"] != "b":
28  isSuccess = False
29  print "Error: Incorrect native call for threading timer"
30 
31  wrapTeapot = localEnv["wrapObj"]
32  if localEnv["nativeCallWithWrapperObjTagA"] != 10 or wrapTeapot is None or wrapTeapot.Name != localEnv["nativeCallWithWrapperObjName"]:
33  isSuccess = False
34  print "Error: Incorrect native call with wrapper object for threading timer"
35 
36  return isSuccess
37 
38 
39 def main():
40  global nativeCallTagA, nativeCallTagB
41  global nativeCallWithWrapperObjTagA, wrapTeapot
42 
43  wrapTeapot = pymxs.runtime.Teapot()
44 
45  # test native call
46  nativeCallTimer = threading.Timer(0.1, nativeCall, [10, "b"], kwargs={"localEnv": globalArgs})
47  nativeWithWrapperTimer = threading.Timer(0.1, nativeWithWrapper, kwargs={
48  "a": 10, "wrapObj": wrapTeapot, "localEnv": globalArgs})
49  print "Start timer"
50  nativeCallTimer.start()
51  nativeWithWrapperTimer.start()
52  time.sleep(0.2)
53  if check(globalArgs):
54  print "threading timer success"
55 
56 if __name__ == "__main__":
57  main()