Multithreaded COM server problem...

From: John Lull (jl_at_windsmith.net)
Date: 01/10/04


Date: 10 Jan 2004 10:44:09 -0600

I'm writing a multithreaded COM server to manage a pool of hardware resources.
All objects are designed to be thread-safe, and I've set sys.coinit_flags to
COINIT_MULTITHREADED before importing pythoncom.

The problem is that all requests to the server seem to be serialized by COM. To
demonstrate the problem, I'm including a simple test server that exposes one
interface object, PyComThreads.Application, with a single method sleep(delay).
I'm also including 2 test programs -- test20.py uses the server to delay 20
seconds, and test1.py delays only one second. The server prints status messages
to the trace collector debugging tool when creating the Application object, and
at the beginning and end of the specified delay.

When I run the 20-second test program, then a couple seconds later run the
1-second test program, I had expected to see something like this:

  Object 8087872 in thread 8160416 created
  Object 8087872 in thread 8160416 delays 20 seconds
  Object 8086008 in thread 8156272 created
  Object 8086008 in thread 8156272 delays 1 seconds
  Object 8086008 delay ends
  Object 8087872 delay ends

Instead, I see:

  Object 8087872 in thread 8160416 created
  Object 8087872 in thread 8160416 delays 20 seconds
  Object 8087872 delay ends
  Object 8086008 in thread 8160416 created
  Object 8086008 in thread 8160416 delays 1 seconds
  Object 8086008 delay ends

Apparently the requests from both client applications are being serialized by
the COM interface.

I need each request (or at least each interface object) to run in its own
thread, or in a pool of threads, and haven't been able to figure out how to
accomplish this. Any suggestions would be appreciated.

Regards,
John

----------
PyComThreads.py:

import sys
import threading
from time import sleep
import win32traceutil

sys.coinit_flags = 0 # 0 == pythoncom.COINIT_MULTITHREADED # !!!!!
import pythoncom

class Application:
    """ Test version of a Multi-threaded local server """

    _reg_progid_ = 'PyComThreads.Application'
    _reg_verprogid_ = 'PyComThreads.Application.100'
    _reg_clsctx_ = pythoncom.CLSCTX_LOCAL_SERVER
    _reg_clsid_ = '{56BEC27D-EDC4-43A0-AEB7-77E4A1381C0F}'

    _public_methods_ = ['sleep']
    _public_attrs_ = []
    _readonly_attrs_ = []

    def __init__(self):
        print 'Object %s in thread %s created' % \
              (id(self), id(threading.currentThread()))

    def sleep(self, delay):
        print 'Object %s in thread %s delays %s seconds' % \
              (id(self), id(threading.currentThread()), delay)
        sleep(delay)
        print 'Object %s delay ends' % id(self)

# COM server registration, etc.
if __name__ == '__main__':
    if hasattr(sys, 'argv'):
        # If *no* command-line arguments, we were not invoked as a server.
        # Assume the user wants us to self-register.
        if len(sys.argv) == 1:
            sys.argv.append('--register')

    import win32com.server.register
    win32com.server.register.UseCommandLine(Application, debug=0)

----------
test20.py:

from win32com.client import Dispatch
app=Dispatch('PyComThreads.Application')
app.sleep(20)

----------
test1.py:

from win32com.client import Dispatch
app=Dispatch('PyComThreads.Application')
app.sleep(1)



Relevant Pages

  • Re: Outlook wont send large attachments
    ... the one operating your own personal mail server. ... header to the e-mail client while they interrogate the message but that only ... the timeout due to excessive delay. ...
    (microsoft.public.outlook)
  • Re: CSocket, best way to wait for a reply?
    ... a synchronous-socket program, and done it so badly it was unbelievable. ... And note that those which can have delay, such as I/O, actually have ... >>> If I 'ask' a question to the server how else would I wait for my reply. ... MVP Tips: http://www.flounder.com/mvp_tips.htm ...
    (microsoft.public.vc.mfc)
  • Re: WTSOpenServer() - WtsAPI32
    ... No, no delay locally. ... I'm running a Win XP Pro workstation, and my WTS ... Now I just need to figure out how to associate the server name (which I have ... in an ArrayList) with the WTS server handle that the WTSOpenServer() ...
    (microsoft.public.windows.terminal_services)
  • Re: Troubleshooting that lousy "Requesting Data..." dialog box in OLXP
    ... This even happens when we're on the same network segment. ... Does increasing the timeout simply delay the box popping up or does it have ... Is there any reason why Outlook 98 did not have this issue? ... What about the server ...
    (microsoft.public.exchange.connectivity)
  • Re: Updates to Delay Calculation in Multicast Mode
    ... delay calculation. ... server in order to authenticate the server, set the time, and calculate the ... I've noticed that clients do not perform any additional roundtrip delay ...
    (comp.protocols.time.ntp)