bug openning Word in a thread, using lock

Vincent L lapointe_vincent_1975 at hotmail.com
Fri Mar 14 12:49:00 EST 2003


Hi,

I'm using: 
  - win32com.client.Dispatch 
method to open Word Application in a thread called "myThread". I'm
also using:
  - thread.Lock 
object to synchronize the main thread of Python with my Thread. But
when the dispatch method is called, the dispatch method hangs (didn't
exit or didn't return).

See below for a sample of my code. I tryed this code on python 2.1 and
2.2.2, pythonwin 150, Word 2000 SP1 and SP3

Notes:
1. If I debug this function using the "step in" command (F10), there
is no problem.
2. If I replace Microsoft.Word by Microsoft.Excel or by Matlab, there
is no problem.
3. If I replace the second acquire(1) command by acquire(0), there is
no problem (but of course no synchronization)
4. I tryed to use Semaphore, RLock and Condition instead of lock but
there is no difference.

Any idea?
Thanks.

Vincent L

#--------------------------------------------------
#--------------------------------------------------

import threading, thread
from time import sleep
from pythoncom import CoInitialize, CoUninitialize
from win32com.client import Dispatch

def threadFunc(lock):
    CoInitialize()
    #open word application
    word = Dispatch('Word.Application')
    #tell to main function that word was open
    lock.release()
    #the thread continu to run any code, and then exit later 
    x = 0
    while x < 10:
        sleep(1)
        x += 1
    #then the thread stop
    word.Quit()
    del word
    CoUninitialize()
    
def mainFunc():
    #initialization: create a lock
    lock = threading.Lock()
    lock.acquire(1)

    #start thread
    thread.start_new_thread(threadFunc,(lock,))
    #wait until word was open
    lock.acquire(0)
    
if __name__ == "__main__":
    #start main script
    mainFunc()
#--------------------------------------------------
#--------------------------------------------------




More information about the Python-list mailing list