Re: [Soya-user] How to stop the mainloop from a second thread?

2008-09-10 Thread Bastian Venthur


Bastian Venthur schrieb:
 Hi
 
 assuming my application runs in two threads: one for the soya stuff and
 the other to steer the first thread -- how to do I stop the
 soya-mainloop running in the first thread from the second thread?
 
 I've currently two solutions:
 
 Solution 1:
 
 Thread2 sets a shared variable which is checked in Thread1 (eg in a
 begin_round)
 
 If Thread1 detects, that this variable is set, it calls
 soya.MAIN_LOOP.stop()
 
 Problem: It takes a while (sometimes minutes) until the main-loop quits.
 I suppose the problem is, that Thread1 which runs the mainloop eats up
 all the CPU cycles and it just takes some random amount of time until
 Thread2 can actually set the variable.

Actually this solution works pretty well, I had a bug in my code which
stopped the mainloop only if there was something in soya's event queue.

My problem seems to be solved with solution 1, but let me know if you
have a better idea.


Cheers,

Bastian
-- 
Bastian Venthur  http://venthur.de
Debian Developer venthur at debian org


___
Soya-user mailing list
Soya-user@gna.org
https://mail.gna.org/listinfo/soya-user


Re: [Soya-user] How to stop the mainloop from a second thread?

2008-09-10 Thread Mark Williamson
Perhaps you could try yielding the CPU in begin_round occasionally to allow 
the other thread to run for a moment?  I'm not sure what the Python way to do 
this (not the yield statement, which does something else!).

Do you *really* need two threads here, or could you perhaps incorporate thread 
2's job into the main loop somehow?

Cheers,
Mark



 What would be your solution to stop the soya-thread from a second one,
 without losing performance in the rendering?


 Cheers,

 Bastian


___
Soya-user mailing list
Soya-user@gna.org
https://mail.gna.org/listinfo/soya-user


Re: [Soya-user] How to stop the mainloop from a second thread?

2008-09-10 Thread snaipperi
Howabout;

import threading
import soya

class soyathread:
def __init__(self):
soya.init()
soya.path.append(os.path.dirname(sys.argv[0]))
self.world = soya.World()
 etc whatever you wish to do.. set self.cam and such

def run(self):
soya.set_root_widget(self.cam)
self.idler = soya.Idler(self.world)
self.idler.idle()

class mainthread(threading.Thread):
def __init__(self,soyaref):
threading.Thread.__init__(self)
self.soyaref = soyaref
def run(self):
time.sleep(1)
while 1:
IO = raw_input('\r\nType 'stop' to stop soya ')
if IO == 'stop': self.soyaref.idler.stop() # soya idler quits.
if __name__ == '__main__':
soyathread = soyathread()
mainthread = mainthread(soyathread)
mainthread.start()
soyathread.run()


There are even better ways to do this so you can stop rendering to
save cpu (and so you can resume it later). Can't remember how I did
years back. But I assume this is enough for you since you just want to
stop the thread.


On 9/10/08, Bastian Venthur [EMAIL PROTECTED] wrote:
 Hi

 assuming my application runs in two threads: one for the soya stuff and
 the other to steer the first thread -- how to do I stop the
 soya-mainloop running in the first thread from the second thread?

 I've currently two solutions:

 Solution 1:

 Thread2 sets a shared variable which is checked in Thread1 (eg in a
 begin_round)

 If Thread1 detects, that this variable is set, it calls
 soya.MAIN_LOOP.stop()

 Problem: It takes a while (sometimes minutes) until the main-loop quits.
 I suppose the problem is, that Thread1 which runs the mainloop eats up
 all the CPU cycles and it just takes some random amount of time until
 Thread2 can actually set the variable.


 Solution 2:

 Instead of running the mainloop in Thread1, I just call the update
 method repeatedly:

   while not stopping:
   time.sleep(0.025)  # 40 FPS
   my_mainloop.upate()

 the sleep call assures that Thread2 doesn't starve. Setting stopping
 in Thread2 has immediate effect on Thread1.

 Problem: due the sleep call (no matter how small the value is -- even 0)
 causes the rendered output to judder -- it looks like 4 FPS not 40.


 What would be your solution to stop the soya-thread from a second one,
 without losing performance in the rendering?


 Cheers,

 Bastian


 --
 Bastian Venthur  http://venthur.de
 Debian Developer venthur at debian org



 ___
 Soya-user mailing list
 Soya-user@gna.org
 https://mail.gna.org/listinfo/soya-user


___
Soya-user mailing list
Soya-user@gna.org
https://mail.gna.org/listinfo/soya-user