在Windows上为python编译线程安全tcl [英] Compile thread-safe tcl for python on Windows

查看:120
本文介绍了在Windows上为python编译线程安全tcl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Python做一个项目,我需要在线程中放入一些东西.事实证明,如果您做一些在线程中使用Tk的操作,它将以某种方式崩溃. 错误是:

I'm doing a project with Python and I need to put something in thread. It turned out that if you do something that uses Tk in thread, it will somehow crash. The error is:

TclError: out of stack space (infinite loop?)`

我在Google上进行了搜索,我认为这可能是因为Tcl不是线程安全的.当我运行它时,我得到了Tcl错误:

I searched on Google and I think this perhaps because Tcl is not thread-safe. When I ran this I got the Tcl error:

import Tkinter    
Tkinter.Tk().getvar("tcl_platform(threaded)")

据说用--enable-threads重新编译tcl可以解决此问题.我的问题是如何在Windows中重新编译tcl.以及如何用已编译的版本替换当前版本.我正在使用Python 2.7和Tcl 8.5

It is said recompiling tcl with --enable-threads could fix this problem. My question is how to recompiling tcl in Windows. And how to replace the current one with the compiled one. I'm using Python 2.7 and Tcl 8.5

谢谢

推荐答案

摘要:每个Tk小部件只能在单个线程中使用;请参见在实现中有很多线程专用的数据在使用,所以这是一个非常困难的要求.您的骇客无法解决这个问题.

Summary: Each Tk widget must only be used from a single thread; there's a lot of thread-specific data in use inside the implementation, so that's a really hard requirement. Your hacking is not going to get around this.

详细信息: Python在幕后与Tcl进行通信以使用Tk,并且线程化Tcl被设计为强线程绑定的(以避免产生类似GIL之类的东西).可以使用非线程构建,但是随后您会遇到代码问题,以防止堆栈溢出(在后台进行非常讨厌的黑客攻击),当它认为它是非线程的时,就会由于存在多个C堆栈而感到困惑. 这是您报告的特定错误所来自的部分. Tcl 8.6(使用无堆栈"实现)中删除了堆栈检查代码,但这可能无济于事因为除非您处于线程构建中(否则您将回到线程特定的数据问题中),否则不会尝试处理线程间锁定问题.

Details: Python communicates with Tcl under the covers to work with Tk, and threaded Tcl is designed to be strongly thread-bound (so as to avoid having things like the GIL). It's possible to use a non-threaded build, but then you get hit by problems with the code to guard against stack overflows (a very nasty hack under the covers) which gets confused by the existence of multiple C stacks when it thinks it is unthreaded. This is the part that particular error you report comes from. The stack check code is removed in Tcl 8.6 (which uses a "stackless" implementation) but that's not likely to help as no attempts are made to deal with inter-thread locking issues unless you're in a threaded build (and that takes you back into thread-specific data issues).

处于非线程模式的Tk对全局共享数据进行了许多假设,因此在多个线程中使用时真的很不安全(所涉及的线程代码的质量不是您所希望的,其中很多时间可以追溯到底层OS库通常也不擅长线程处理).线程模式下的Tk广泛使用TSD;在线程模式下使用Tk的唯一方法是使每个线程都有自己的主窗口和事件循环,并且从不将它们之间的内容混合在一起.

Tk in unthreaded mode makes lots of assumptions about global shared data and is so really unsafe to use from multiple threads (the quality of the threading code involved is not what you might wish, with much of it dating from a time when the underlying OS libraries were often not very good at thread handling either). Tk in threaded mode uses TSD extensively; the only way to use Tk in threaded mode is for each thread to have its own main window and event loop, and to never mix things up between them.

最简单的方法是保留一个专用于管理GUI的线程(通常是主应用程序线程),并将所有非GUI的工作移到其他线程中.

The easiest thing is to keep one thread dedicated to managing the GUI (often the main application thread) and to move all non-GUI work into other threads.

这篇关于在Windows上为python编译线程安全tcl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆