gpt4 book ai didi

python - 如何在 python 中进行线程处理?

转载 作者:行者123 更新时间:2023-12-01 06:16:08 26 4
gpt4 key购买 nike

我正在尝试学习如何在 python 中使用线程。这是我一直在研究的代码:

import time
from threading import Thread

def myfunc(i):
print "sleeping 5 sec from thread %d" % i
time.sleep(5)
print "finished sleeping from thread %d" % i

for i in range(10):
t = Thread(target=myfunc, args=(i,))
t.start()

程序在命令提示符下运行良好,但是当我尝试在空闲状态下运行它时,我收到如下错误:

Traceback (most recent call last):
File "C:\Python24\lib\lib-tk\Tkinter.py", line 1345, in __call__
return self.func(*args)
File "C:\Python24\lib\idlelib\ScriptBinding.py", line 165, in run_module_event
interp.runcode(code)
File "C:\Python24\lib\idlelib\PyShell.py", line 726, in runcode
self.tkconsole.endexecuting()
File "C:\Python24\lib\idlelib\PyShell.py", line 901, in endexecuting
self.showprompt()
File "C:\Python24\lib\idlelib\PyShell.py", line 1163, in showprompt
self.resetoutput()
File "C:\Python24\lib\idlelib\PyShell.py", line 1178, in resetoutput
self.text.insert("end-1c", "\n")
File "C:\Python24\lib\idlelib\Percolator.py", line 25, in insert
self.top.insert(index, chars, tags)
File "C:\Python24\lib\idlelib\PyShell.py", line 315, in insert
UndoDelegator.insert(self, index, chars, tags)
File "C:\Python24\lib\idlelib\UndoDelegator.py", line 81, in insert
self.addcmd(InsertCommand(index, chars, tags))
File "C:\Python24\lib\idlelib\UndoDelegator.py", line 116, in addcmd
cmd.do(self.delegate)
File "C:\Python24\lib\idlelib\UndoDelegator.py", line 216, in do
if text.compare(self.index1, ">", "end-1c"):
File "C:\Python24\lib\lib-tk\Tkinter.py", line 2784, in compare
return self.tk.getboolean(self.tk.call(
TclError: expected boolean value but got ""

Python 线程是不稳定还是我做错了什么?该示例来自:http://www.saltycrane.com/blog/2008/09/simplistic-python-thread-example/

最佳答案

这听起来像是 IDLE 中的错误,而不是 Python 的问题。该错误来自 Tkinter,它是一个 Python GUI 工具包,IDLE 可能使用它。我会将其报告给维护 IDLE 的人。

关于python - 如何在 python 中进行线程处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3257834/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com