Apartments Open for Inspection

It’s time to demonstrate some of these concepts. To do this, we develop three COM objects, each of which support one of the various threading models discussed previously. These COM objects are quite simple and expose only two methods: GetCreatedThreadId() to return the thread ID of the thread that created the object, and GetCurrentThreadId() to return the thread ID of the current thread (that is, the thread receiving the call). If you have read Chapter 12, there will be nothing new in this example. The only points worth mentioning are that we use win32api.GetCurrentThreadId() to obtain the Win32 Thread ID, and that we use a Python base class for the raw COM functionality, and subclasses for the object-specific registration information. The COM objects are implemented in ThreadingModelsServer.py :

# ThreadingModelsServer.py # Python COM objects that demonstrate COM threading models. # # Exposes 3 Python objects, all of which have identical functionality, # but each indicate they support different threading models. import win32api # A Base class for our 2 trivial objects. class ThreadDemoObject: _public_methods_ = [ 'GetCurrentThreadId', 'GetCreatedThreadId' ] def __init__(self): self.created_id = win32api.GetCurrentThreadId() def GetCreatedThreadId(self): return self.created_id def GetCurrentThreadId(self): # Simply return an integer with the Win32 thread ID. return win32api.GetCurrentThreadId() class ThreadApartmentObject(ThreadDemoObject): _reg_threading_ ...

Get Python Programming On Win32 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.