【问题标题】:Python, thread-function doesn't execute and the object disappearsPython,线程函数不执行,对象消失
【发布时间】:2014-09-29 18:03:31
【问题描述】:

我们正在尝试在 Python 中运行此代码,但是当函数 print_time 工作时,TestReactionTime 不执行(它甚至不打印)。 对这两个函数的调用是相同的。

另外,独立地,当我们第一次释放球并尝试再次抓住它时,它会消失。

任何问题的帮助将不胜感激。

(我们使用的程序是Vizard)

import viz
import math
import viztask
import vizinfo
import thread
import time

count = 0
boolTime = False

viz.setMultiSample(4)
viz.fov(20)
viz.go()

viz.phys.enable()
viz.phys.setGravity( [0, 0, 0] )
viz.window.setFullscreen()

viz.setOption('viz.model.apply_collada_scale',1)
ball = viz.add('ball.dae')
ball.setPosition([-0.1,1.5,4])
#ball.setScale([0.75,0.75,0.75])
ball.collideSphere()


viz.setOption('viz.model.apply_collada_scale',1)
path = viz.addChild('path.dae')
path.setPosition([-1,1.0,4])
path.collideMesh()

#collision
path.enable(viz.COLLIDE_NOTIFY)
def onCollide(e):
        global count 
        count = count+1
        print(count)

viz.callback( viz.COLLIDE_BEGIN_EVENT, onCollide )

#mouse
viz.mouse.setOverride(viz.ON) 
link = None 

**def TestReactionTime(threadName):**
    print 'boolTime: '
    print(boolTime)
    while boolTime:
        #Wait for next frame to be drawn to screen
        d = yield viztask.waitDraw()

        #Save display time
        displayTime = d.time

        #Wait for keyboard reaction
        d = yield viztask.waitMouseUp(viz.MOUSEBUTTON_LEFT)

        #Calculate reaction time
        reactionTime = d.time - displayTime
        print(reactionTime)

def print_time( threadName, delay):
   count = 0
   while count < 5:
      time.sleep(delay)
      count += 1
      print "%s: %s" % ( threadName, time.ctime(time.time()) )

def grabBall():
    global link
    global boolTime
    boolTime = True
    print("grab ")
    print(boolTime)
    try:
        #thread.start_new_thread( TestReactionTime,() )
        thread.start_new_thread( TestReactionTime, ("Thread-3", ) )
        thread.start_new_thread( print_time, ("Thread-1", 2, ) )
        print("execute thread")
    except:
        print "Error: unable to start thread"
    link = viz.grab( viz.Mouse, ball )


def releaseBall():
    global link,boolTime
    boolTime = False
    link.remove()
    link = None

vizact.onmousedown(viz.MOUSEBUTTON_LEFT,grabBall)
vizact.onmouseup(viz.MOUSEBUTTON_LEFT,releaseBall)

【问题讨论】:

    标签: python multithreading function vizard


    【解决方案1】:

    一旦你创建了线程,你应该join它们,以便主程序等待每个线程完成执行。

    thread 模块不提供线程等待的任何规定。您应该使用推荐的Threading 模块

    thread.start_new_thread 调用更改为Thread()

        from threading import Thread
    
        #do something
    
        try:
           thread1 = Thread( target = TestReactionTime, args = ("Thread-3", ) )
           thread2 = Thread( target = print_time, args = ("Thread-1", 2, ) )
    
           thread1.start()
           thread2.start()
    
           thread1.join()
           thread2.join()
    
       #do the rest
    

    【讨论】:

    • 谢谢,@nu11p01n73R,我们改变了这一点。但它仍然没有进入 TestReactionTime 函数。它甚至不打印“booltime”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-15
    • 2021-12-19
    • 2021-09-26
    • 1970-01-01
    相关资源
    最近更新 更多