批处理之家's Archiver

keshun 发表于 2021-10-21 20:18

Python报读慢造成子线程堵塞报错,请教高手?【己解决】

[i=s] 本帖最后由 keshun 于 2021-11-5 10:47 编辑 [/i]

[code]import win32com.client, pythoncom, threading, time

def aa():
    pythoncom.CoInitialize()
    speak = win32com.client.Dispatch('SAPI.SPVOICE')
    speak.Speak(a)

a='1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20'

for i in range(10):
    threading.Thread(target=aa).start()
    for j in range(5):
        time.sleep(1)
        print('{}{}'.format(i,j))

input("关闭窗口")[/code]报读慢造成子线程堵塞报错,请教高手?

keshun 发表于 2021-10-22 07:25

[i=s] 本帖最后由 keshun 于 2021-10-22 07:27 编辑 [/i]

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=247115&ptid=60467]1#[/url] [i]keshun[/i] [/b]


    代码运行子线程报读10次,其中5次堵塞报错,最终只报读了5次,主线程正常运行。
以下是第3次报读报错信息,求高手指教??
Exception in thread Thread-3:
Traceback (most recent call last):
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "D:\桌面\1.py", line 6, in aa
    speak.Speak(a)
  File "<COMObject SAPI.SPVOICE>", line 2, in Speak
pywintypes.com_error: (-2147352567, '发生意外。', (0, None, None, None, 0, -2147201018), None)

keshun 发表于 2021-11-5 10:47

[code]try: speak.Speak('请注意:{}'.format(q4))
except: pass
else: pass[/code]线程堵塞时自动跳过算了,必竟代码报读集中一起的时候不常出现。

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.