home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_win32com_test_testMarshal.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  4.3 KB  |  123 lines

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