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.

testMarshal.py 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 threading
  18. import unittest
  19. import pythoncom
  20. import win32api
  21. import win32com.client
  22. import win32event
  23. from .testServers import InterpCase
  24. freeThreaded = 1
  25. class ThreadInterpCase(InterpCase):
  26. def _testInterpInThread(self, stopEvent, interp):
  27. try:
  28. self._doTestInThread(interp)
  29. finally:
  30. win32event.SetEvent(stopEvent)
  31. def _doTestInThread(self, interp):
  32. pythoncom.CoInitialize()
  33. myThread = win32api.GetCurrentThreadId()
  34. if freeThreaded:
  35. interp = pythoncom.CoGetInterfaceAndReleaseStream(
  36. interp, pythoncom.IID_IDispatch
  37. )
  38. interp = win32com.client.Dispatch(interp)
  39. interp.Exec("import win32api")
  40. # print "The test thread id is %d, Python.Interpreter's thread ID is %d" % (myThread, interp.Eval("win32api.GetCurrentThreadId()"))
  41. pythoncom.CoUninitialize()
  42. def BeginThreadsSimpleMarshal(self, numThreads):
  43. """Creates multiple threads using simple (but slower) marshalling.
  44. Single interpreter object, but a new stream is created per thread.
  45. Returns the handles the threads will set when complete.
  46. """
  47. interp = win32com.client.Dispatch("Python.Interpreter")
  48. events = []
  49. threads = []
  50. for i in range(numThreads):
  51. hEvent = win32event.CreateEvent(None, 0, 0, None)
  52. events.append(hEvent)
  53. interpStream = pythoncom.CoMarshalInterThreadInterfaceInStream(
  54. pythoncom.IID_IDispatch, interp._oleobj_
  55. )
  56. t = threading.Thread(
  57. target=self._testInterpInThread, args=(hEvent, interpStream)
  58. )
  59. t.setDaemon(1) # so errors dont cause shutdown hang
  60. t.start()
  61. threads.append(t)
  62. interp = None
  63. return threads, events
  64. #
  65. # NOTE - this doesnt quite work - Im not even sure it should, but Greg reckons
  66. # you should be able to avoid the marshal per thread!
  67. # I think that refers to CoMarshalInterface though...
  68. def BeginThreadsFastMarshal(self, numThreads):
  69. """Creates multiple threads using fast (but complex) marshalling.
  70. The marshal stream is created once, and each thread uses the same stream
  71. Returns the handles the threads will set when complete.
  72. """
  73. interp = win32com.client.Dispatch("Python.Interpreter")
  74. if freeThreaded:
  75. interp = pythoncom.CoMarshalInterThreadInterfaceInStream(
  76. pythoncom.IID_IDispatch, interp._oleobj_
  77. )
  78. events = []
  79. threads = []
  80. for i in range(numThreads):
  81. hEvent = win32event.CreateEvent(None, 0, 0, None)
  82. t = threading.Thread(target=self._testInterpInThread, args=(hEvent, interp))
  83. t.setDaemon(1) # so errors dont cause shutdown hang
  84. t.start()
  85. events.append(hEvent)
  86. threads.append(t)
  87. return threads, events
  88. def _DoTestMarshal(self, fn, bCoWait=0):
  89. # print "The main thread is %d" % (win32api.GetCurrentThreadId())
  90. threads, events = fn(2)
  91. numFinished = 0
  92. while 1:
  93. try:
  94. if bCoWait:
  95. rc = pythoncom.CoWaitForMultipleHandles(0, 2000, events)
  96. else:
  97. # Specifying "bWaitAll" here will wait for messages *and* all events
  98. # (which is pretty useless)
  99. rc = win32event.MsgWaitForMultipleObjects(
  100. events, 0, 2000, win32event.QS_ALLINPUT
  101. )
  102. if (
  103. rc >= win32event.WAIT_OBJECT_0
  104. and rc < win32event.WAIT_OBJECT_0 + len(events)
  105. ):
  106. numFinished = numFinished + 1
  107. if numFinished >= len(events):
  108. break
  109. elif rc == win32event.WAIT_OBJECT_0 + len(events): # a message
  110. # This is critical - whole apartment model demo will hang.
  111. pythoncom.PumpWaitingMessages()
  112. else: # Timeout
  113. print(
  114. "Waiting for thread to stop with interfaces=%d, gateways=%d"
  115. % (pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount())
  116. )
  117. except KeyboardInterrupt:
  118. break
  119. for t in threads:
  120. t.join(2)
  121. self.assertFalse(t.is_alive(), "thread failed to stop!?")
  122. threads = None # threads hold references to args
  123. # Seems to be a leak here I can't locate :(
  124. # self.assertEqual(pythoncom._GetInterfaceCount(), 0)
  125. # self.assertEqual(pythoncom._GetGatewayCount(), 0)
  126. def testSimpleMarshal(self):
  127. self._DoTestMarshal(self.BeginThreadsSimpleMarshal)
  128. def testSimpleMarshalCoWait(self):
  129. self._DoTestMarshal(self.BeginThreadsSimpleMarshal, 1)
  130. # def testFastMarshal(self):
  131. # self._DoTestMarshal(self.BeginThreadsFastMarshal)
  132. if __name__ == "__main__":
  133. unittest.main("testMarshal")