K
K
Konstantin Dolinin2015-03-11 19:45:35
Python
Konstantin Dolinin, 2015-03-11 19:45:35

How to use win32com.client from thread?

There are two codes, one works:

#!/usr/bin/python2
# -*- coding: cp1251 -*-

from win32com.client import constants
import win32com.client

sapi = win32com.client.Dispatch("SAPI.SpVoice")
phrase = "Привет, мир!"
sapi.Speak(phrase, constants.SVSFlagsAsync)

And the other is not:
#!/usr/bin/python2
# -*- coding: cp1251 -*-

from thread import *
from win32com.client import constants
import win32com.client

def thread():
    sapi = win32com.client.Dispatch("SAPI.SpVoice")
    phrase = "Привет, мир!"
    sapi.Speak(phrase, constants.SVSFlagsAsync)

start_new_thread(thread, ())

It is required, well, it is very necessary to run these functions from the thread (the connected client is spinning there). How?
I am a beginner in python, to put it mildly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
George, 2015-03-11
@kostett

I can't test it right now, but in theory it should work like this:

#!/usr/bin/python2
# -*- coding: cp1251 -*-

import pythoncom
from thread import *
from win32com.client import constants
import win32com.client

def thread():
    sapi = win32com.client.Dispatch("SAPI.SpVoice")
    phrase = "Привет, мир!"
    pythoncom.CoInitialize()
    sapi.Speak(phrase, constants.SVSFlagsAsync)
    pythoncom.CoUninitialize()

start_new_thread(thread, ())

Or somehow in this spirit.
pythoncom lives in the pywin32 package

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question