List Info

Thread: Re: Reusing a local com server object




Re: Reusing a local com server object
country flaguser name
United States
2007-03-06 12:28:23
Ross McKerchar wrote:
>I am having trouble connecting to an already running
python com server.
> 
> I have set CLSCTX_LOCAL_SERVER for my com object and I
am using the 
> "GetObject" function in my vbscripts which
connect to the com server.
> 
> I have written a simple test server that behaves very
similarly to my 
> real server (except the thread will actually do some
work)
> 
>
------------------------------------------------------------

> sleeper =
threading.Thread(target=time.sleep,args=(20,))
> 
> class Test(object):
>    _public_methods_ = ["go"]
>    _reg_progid_ = "pythonutils.test"
>    _reg_clsid_ =
""
>    _reg_clsctx_ = pythoncom.CLSCTX_LOCAL_SERVER
> 
>    def go(self):
>        if sleeper.isAlive():
>            return "Server is already
running"
>        else:
>            sleeper.start() #start a thread that sleep
for 20 seconds
>            return "Server NOT running"
>
------------------------------------------------------------

> 
> Now, if I run two test clients it works fine if client
#2 get's a 
> reference to my com server before client #1 stops
running (i.e. client#1 
> sleeps for 10 seconds and I run client#2, in a seperate
process before 
> client#1 finishes).
> 
> However, if I run my test clients sequentially each one
creates a new 
> pythonw process. The old com server is definitely still
running, 
> GetObject just decides to return a new one and I am
unsure how the 
> change this behaviour.
> 
> After browsing Mark's win32 book I've tried playing
with the 
> _reg_threading_ but I'm not convinced it's relevant (I
full admit I'm 
> dont understand com threading 100%). I also thought I
was onto something 
> with the pythoncom.REGCLS_MULTIPLEUSE variable until I
discovered that 
> this is used by default (in win32com.server.factory).
> 
> Any suggestions would be much appreciated - even just
some key phrases 
> that may help energise my search efforts...


An object needs to be registered in the Running Object Table
(ROT) for this to
work. See pythoncom.RegisterActiveObject.

       hth
          Roger

_______________________________________________
Python-win32 mailing list
Python-win32python.org

http://mail.python.org/mailman/listinfo/python-win32

Re: Reusing a local com server object
country flaguser name
United Kingdom
2007-03-07 04:05:04
Roger Upole wrote:
> Ross McKerchar wrote:
>   
>> Any suggestions would be much appreciated - even
just some key phrases 
>> that may help energise my search efforts...
>>     
> An object needs to be registered in the Running Object
Table (ROT) for this to
> work. See pythoncom.RegisterActiveObject.
>   
Ta very much - that's exactly the missing snippet I was
looking for.

For posterity, here's my updated test solution:

-----------------------------------------------
class TestThread(threading.Thread):
    def run(self):
        pythoncom.CoInitialize()
        wrapped = win32com.server.util.wrap(self.comobj)
        handle = pythoncom.RegisterActiveObject(wrapped, 
self.comobj._reg_clsid_, 0)
        time.sleep(20)
        pythoncom.RevokeActiveObject(handle)

class FaxJob(object):
    _public_methods_ = ["go"]
    _reg_progid_ = "pythonutils.test"
    _reg_clsid_ =
""
    _reg_clsctx_ = pythoncom.CLSCTX_LOCAL_SERVER
    testthread = TestThread()
   
    def go(self):
        if self.testthread.isAlive():
            return "Server is already running"
        else:
            self.testthread.comobj = self
            self.testthread.start()
            return "Starting new server"
-----------------------------------------------

Many thanks.

-ross
_______________________________________________
Python-win32 mailing list
Python-win32python.org

http://mail.python.org/mailman/listinfo/python-win32

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )