【问题标题】:Python multithreading - closing thread in BlenderPython多线程 - Blender中的关闭线程
【发布时间】:2013-02-10 17:36:38
【问题描述】:

我有一个小问题,在搅拌机游戏引擎中使用多线程 python 脚本。 它工作正常,但是当我停止游戏时,它会引发一些异常并且有时会崩溃。

from bge import logic
import time
from threading import Thread

def init():

    if not hasattr(logic, 'init'):
        logic.init = 0
        logic.thread = new()
        logic.thread.start()

    logic.thread.restart()

class new(Thread):
    def __init__(self):
        self.Thread = Thread
        self.Thread.__init__(self)
    def run(self):
        number = 0
        while 1:
            number += 1
            print(number)
            try:
                main()
                time.sleep(0.1)
            except:
                break

    def restart(self):
        self.Thread.__init__(self)

def main(): #this part isn't important now ...
    cam = bge.logic.getCurrentScene().active_camera
    obj = bge.logic.getCurrentController().owner
    obj.worldPosition.x = cam.worldPosition.x
    obj.worldPosition.y = cam.worldPosition.y

控制台写道:

Unhandled exception in thread started by <bound method new._bootstrap of <new(Th
read-80, initial)>>
Traceback (most recent call last):
  File "C:\Program Files (x86)\Blender Foundation\Blender\2.64\python\lib\thread
ing.py", line 709, in _bootstrap
    self._bootstrap_inner()
  File "C:\Program Files (x86)\Blender Foundation\Blender\2.64\python\lib\thread
ing.py", line 784, in _bootstrap_inner
    with _active_limbo_lock:
AttributeError: __exit__

如果有人能找出问题所在,我会很高兴。 谢谢

【问题讨论】:

  • 您能否在问题中包含您遇到的例外情况?此外,self.Thread = Thread 看起来很奇怪。那条线是干什么用的?
  • 当然,由 > 的 > Traceback(最近一次调用)启动的线程中出现未处理的异常:文件“C:\Program Files (x86)\Blender Foundation\Blender\2.64\python\lib\thread ing.py",第 709 行,在 _bootstrap self._bootstrap_inner() 文件“C:\Program Files (x86)\Blender Foundation\Blender\2.64\python \lib\thread ing.py",第 784 行,在带有 _active_limbo_lock 的 _bootstrap_inner 中:AttributeError: exit
  • 最好将其添加到问题中,以便格式化。
  • 完成,感谢您的宝贵时间 :)
  • 太好了,谢谢。还有一个问题:在您的问题中,您提到了“一些例外 s ”。它们都和这个一样吗?

标签: python multithreading blender python-3.2 python-multithreading


【解决方案1】:

那是well-known limitation of Python scripting in Blender

问题是 Blender 在你的线程之前拆掉了 Python。您可以尝试做的是以某种方式注册 Blender(或您的游戏)正在退出,通知您的线程,并从主线程join 它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    • 2022-01-15
    相关资源
    最近更新 更多