Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

testGIT.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. """Testing pasing object between multiple COM threads
  2. Uses standard COM marshalling to pass objects between threads. Even
  3. though Python generally seems to work when you just pass COM objects
  4. between threads, it shouldnt.
  5. This shows the "correct" way to do it.
  6. It shows that although we create new threads to use the Python.Interpreter,
  7. COM marshalls back all calls to that object to the main Python thread,
  8. which must be running a message loop (as this sample does).
  9. When this test is run in "free threaded" mode (at this stage, you must
  10. manually mark the COM objects as "ThreadingModel=Free", or run from a
  11. service which has marked itself as free-threaded), then no marshalling
  12. is done, and the Python.Interpreter object start doing the "expected" thing
  13. - ie, it reports being on the same thread as its caller!
  14. Python.exe needs a good way to mark itself as FreeThreaded - at the moment
  15. this is a pain in the but!
  16. """
  17. import _thread
  18. import traceback
  19. import pythoncom
  20. import win32api
  21. import win32com.client
  22. import win32event
  23. def TestInterp(interp):
  24. if interp.Eval("1+1") != 2:
  25. raise ValueError("The interpreter returned the wrong result.")
  26. try:
  27. interp.Eval(1 + 1)
  28. raise ValueError("The interpreter did not raise an exception")
  29. except pythoncom.com_error as details:
  30. import winerror
  31. if details[0] != winerror.DISP_E_TYPEMISMATCH:
  32. raise ValueError(
  33. "The interpreter exception was not winerror.DISP_E_TYPEMISMATCH."
  34. )
  35. def TestInterpInThread(stopEvent, cookie):
  36. try:
  37. DoTestInterpInThread(cookie)
  38. finally:
  39. win32event.SetEvent(stopEvent)
  40. def CreateGIT():
  41. return pythoncom.CoCreateInstance(
  42. pythoncom.CLSID_StdGlobalInterfaceTable,
  43. None,
  44. pythoncom.CLSCTX_INPROC,
  45. pythoncom.IID_IGlobalInterfaceTable,
  46. )
  47. def DoTestInterpInThread(cookie):
  48. try:
  49. pythoncom.CoInitialize()
  50. myThread = win32api.GetCurrentThreadId()
  51. GIT = CreateGIT()
  52. interp = GIT.GetInterfaceFromGlobal(cookie, pythoncom.IID_IDispatch)
  53. interp = win32com.client.Dispatch(interp)
  54. TestInterp(interp)
  55. interp.Exec("import win32api")
  56. print(
  57. "The test thread id is %d, Python.Interpreter's thread ID is %d"
  58. % (myThread, interp.Eval("win32api.GetCurrentThreadId()"))
  59. )
  60. interp = None
  61. pythoncom.CoUninitialize()
  62. except:
  63. traceback.print_exc()
  64. def BeginThreadsSimpleMarshal(numThreads, cookie):
  65. """Creates multiple threads using simple (but slower) marshalling.
  66. Single interpreter object, but a new stream is created per thread.
  67. Returns the handles the threads will set when complete.
  68. """
  69. ret = []
  70. for i in range(numThreads):
  71. hEvent = win32event.CreateEvent(None, 0, 0, None)
  72. _thread.start_new(TestInterpInThread, (hEvent, cookie))
  73. ret.append(hEvent)
  74. return ret
  75. def test(fn):
  76. print("The main thread is %d" % (win32api.GetCurrentThreadId()))
  77. GIT = CreateGIT()
  78. interp = win32com.client.Dispatch("Python.Interpreter")
  79. cookie = GIT.RegisterInterfaceInGlobal(interp._oleobj_, pythoncom.IID_IDispatch)
  80. events = fn(4, cookie)
  81. numFinished = 0
  82. while 1:
  83. try:
  84. rc = win32event.MsgWaitForMultipleObjects(
  85. events, 0, 2000, win32event.QS_ALLINPUT
  86. )
  87. if rc >= win32event.WAIT_OBJECT_0 and rc < win32event.WAIT_OBJECT_0 + len(
  88. events
  89. ):
  90. numFinished = numFinished + 1
  91. if numFinished >= len(events):
  92. break
  93. elif rc == win32event.WAIT_OBJECT_0 + len(events): # a message
  94. # This is critical - whole apartment model demo will hang.
  95. pythoncom.PumpWaitingMessages()
  96. else: # Timeout
  97. print(
  98. "Waiting for thread to stop with interfaces=%d, gateways=%d"
  99. % (pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount())
  100. )
  101. except KeyboardInterrupt:
  102. break
  103. GIT.RevokeInterfaceFromGlobal(cookie)
  104. del interp
  105. del GIT
  106. if __name__ == "__main__":
  107. test(BeginThreadsSimpleMarshal)
  108. win32api.Sleep(500)
  109. # Doing CoUninit here stop Pythoncom.dll hanging when DLLMain shuts-down the process
  110. pythoncom.CoUninitialize()
  111. if pythoncom._GetInterfaceCount() != 0 or pythoncom._GetGatewayCount() != 0:
  112. print(
  113. "Done with interfaces=%d, gateways=%d"
  114. % (pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount())
  115. )
  116. else:
  117. print("Done.")