O'Reilly logo

Python Programming On Win32 by Mark Hammond, Andy Robinson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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_ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required