Panda3D Python 线程崩溃

Panda3D Python threading crashes

概览: 我用 PythonPanda3D 创建了一个类似 Minecraft 的生成脚本,我把它放在一个线程上所以我可以使用 Time.Sleep() 和其他函数,但知道线程崩溃的原因吗?

我做了什么: 我用 PythonPanda3D.

创建了一个类似 Minecraft 的生成脚本

问题:我不能在 Panda3D 中使用线程。

这里所有我创建的代码:

from direct.showbase.ShowBase import ShowBase
from panda3d.core import *
import perlin, colorsys, time
import thread

scaleX = 0.05
scaleZ = 0.05
blockSize = 1
size = 16
bottom = -70



waiter = 0.001

sn = perlin.SimplexNoise()

def threaded_generator():
    for x in range(size):
            for z in range(size):
                    time.sleep(waiter)
                    y = sn.noise2(x*scaleX, z*scaleZ)
                    # Load the environment model.
                    cube = loader.loadModel("Test")
                    # Reparent the model to render.
                    cube.reparentTo(render)
                    # Apply scale and position transforms on the model.
                    cube.setScale(blockSize, blockSize, blockSize)
                    cube.setPos(x*blockSize, z*blockSize, round(y))
                    cy = round(y)
                    while cy > bottom:
                        cy -= 1
                        cube = loader.loadModel("Test")
                        # Reparent the model to render.
                        cube.reparentTo(render)
                        # Apply scale and position transforms on the model.
                        cube.setScale(blockSize, blockSize, blockSize)
                        cube.setPos(x*blockSize, z*blockSize, round(cy))


class MyApp(ShowBase):

    def __init__(self):
        ShowBase.__init__(self)
        #Ambient
        alight = AmbientLight('alight')
        alight.setColor(VBase4(0.2, 0.2, 0.2, 1))
        alnp = render.attachNewNode(alight)
        render.setLight(alnp)
        #Directional
        dlight = DirectionalLight('dlight')
        dlight.setColor(VBase4(0.8, 0.8, 0.5, 1))
        dlnp = render.attachNewNode(dlight)
        dlnp.setHpr(0, -60, 0)
        render.setLight(dlnp)
        #Cubes 
        print("Ding!")
        thread.start_new_thread(threaded_generator, ())

app = MyApp()
app.run()

对我来说它开始产生一点,

然后就停了,好像没完,有时候停的挺快,有时候过几秒就停了。

~Coolq:)

Panda3D manual 开始,您必须使用 Panda3D 提供的线程,因为底层的 c++ 结构。

检查线程:

from panda3d.core import Thread
print Thread.isThreadingSupported()

线程的使用

# WRONG:
import thread
# RIGHT:
from direct.stdpy import thread

线程的使用

# WRONG:
import threading
# RIGHT:
from direct.stdpy import threading
# ALSO RIGHT:
from direct.stdpy import threading2 as threading