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

标签 python multithreading

我正在尝试学习如何在 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/

相关文章:

python - Tensorflow 的多输出神经网络

c++ - 在 C++ 中创建线程并出现重载错误

python - python列表括号中的逗号索引

python - 显示并返回函数上重复字符串的数量?

python - 如何在 Python 中使用 argparse 为一个参数设置可变数量的参数?

java - 插入 System.out.println 调用会影响竞争条件 : how to debug?

c++ - OpenMP 矩阵 vector 乘法仅在一个线程上执行

c# - 在 C# 中允许多个读者的锁

multithreading - 来自Task的tableview中的JavaFX更新进度条

python - Python 字典的顺序是否在迭代中得到保证?