Reputation: 317
We are trying to run this code in Python, but while the function print_time works, the TestReactionTime doesn't execute (it doesn't even print). The calls to both functions are identical.
Also, and independently, when we release the ball for the first time and try to grab it again, it disappears.
Any help with any of the problems will be grateful.
(The program we are using is 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)
Upvotes: 0
Views: 1334
Reputation: 26667
Once you create threads, you should join
them so that the main program waits until each thread is completed executing.
The thread
module does not provide any provision for thread to wait. You should be using the recomended Threading
module
change the thread.start_new_thread
calls with the 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
Upvotes: 1