[python-win32] COM with timeouts?

Tim Roberts timr at probo.com
Mon Feb 9 22:17:46 CET 2009


Stephen Hansen wrote:
> Hi all.
>
> I have an application that's controlling Word via COM, and have
> situations where I'd like to set a timeout on those operations. Word
> might throw up an error message for example which stalls everything
> until clicked. In such a case I'd rather just give up, go and kill
> Word and start over.
>
> My first thought was: why, I'll wrap the calls in a context manager
> that sets a SIGALRM. Then remembered Windows didn't support SIGALRM,
> and cried inside.
>
> Any tips on an approach to take to interrupt a COM call or set a timeout on it?
>   

There's no reliable way to do that.  You could, of course, spin off a
separate thread to simulate SIGALRM using time.sleep, but once you had
done so, there's no good way to stop the thread that is blocked waiting
for Word to respond.  And if word has displayed a modal error box, it's
not trivial to kill it in a clean way.

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the python-win32 mailing list