Re: Protecting against callbacks queuing up?
On Friday 28 August 2009 00:42:16 Esben von Buchwald wrote: OK, now things starts to make sense. You tell me to do something like this? def doCallback(self): if self.process_busy==False: self.process_busy=True self.at.after(0.01,self.data_callback) This bit is allmost right - try to make the time a millisecond or less, if you can. (however that is specified - I do not know) The spelling is probably: self.at.after(1,self.data_callback, (other_stuff)) where other stuff comes from the event and is what is needed to do the calculation. - the event should be an argument to the doCallback thinghy too, somehow, so that you can get at what you need to do the calculation. def doneCallback(self): self.process_busy=False you do not need this separate. just do this: def data_callback(self,other_stuff): calculate what must be calculated self.process_busy=False And the after command will cause python to spawn a new thread, which runs self.data_callback, which measn that the doCallback will return immediately, so the next callback from accelerometer can be processed? Yes and skipped if the calculation is not done yet. Obviously you will have to do whatever it takes to make sure the events keep coming (if anything), so it may end up a little more complex, but you basically have the idea now. The thing that happens as a result of the after is not really a new thread, it is a piece of the main loop that gets diverted for a while to do the calculation. if, in that routine, you go: while True: pass You will completely freeze up the whole bang shoot.. And when the self.data_callback() finishes, i'd make it call the doneCallback() to release my busy flag, right? see above - just clear it from the called routine. I'm gonna try this tomorrow :) Good luck! - Hendrik -- http://mail.python.org/mailman/listinfo/python-list
Re: Protecting against callbacks queuing up?
It seems to solve the problem. What I did: def contextDataHandler(self): self.contextdata.process_busy=True self.services.findServices() self.drawDisplay() self.contextdata.process_busy=False def doCallback(self): self.at.cancel() if self.process_busy==False: self.at.after(0.01,self.data_callback) The app works awesomely stable now data_callback refers to contextDataHandler in parent class Thanks guys, good job -- http://mail.python.org/mailman/listinfo/python-list
Re: Protecting against callbacks queuing up?
Hendrik van Rooyen wrote: would that be usable? Probably If so, how? This is a guess, for your device, but I suspect something along these lines: t = Ao_timer() cb = t.after(100,thing_that_does_the_work(with_its_arguments)) Lots of assumptions here - the 100 should give you a tenth of a second... Don't know what the arguments look like, and if you need to pass an instance (like self) - that would depend on where you are calling it from. Play and see :-) You should also be able to cancel the callback like this: t.cancel(cb) If you do it before the time out - Hendrik I don't really get it... I can see that I should put a t.after(...) around the function that does the work, when calling it. That delays each call for the given period. I just tried it out, the console keeps saying - Traceback (most recent call last): File c:\resource\python25\python25.zip\sensorfw.py, line 499, in data_cb File c:\resource\python25\python25.zip\sensorfw.py, line 160, in custom_cb File e:\python\r3s_contextdata.py, line 79, in acc_filter self.acc_callback() File e:\python\r3s_contextdata.py, line 85, in acc_callback self.doCallback() File e:\python\r3s_contextdata.py, line 47, in doCallback self.at.after(0.05,self.data_callback) RuntimeError: Timer pending - cancel first - It seems like i should cancel the current counting timer, when a new call comes in - but how to determine when to cancel etc? I'm kind of new to all this async python stuff. -- http://mail.python.org/mailman/listinfo/python-list
Re: Protecting against callbacks queuing up?
Dennis Lee Bieber wrote: On Tue, 25 Aug 2009 15:21:16 +0200, Esben von Buchwald find@paa.google declaimed the following in gmane.comp.python.general: This is how the accelerometer is accessed http://pys60.garage.maemo.org/doc/s60/node59.html I found this called after... http://pys60.garage.maemo.org/doc/s60/node12.html would that be usable? Based on the documentation... I'd suggest... def doCallback(self): self.accelerometer.stop_listening() self.data_callback() self.accelerometer.start_listening() with appropriate changes to whatever reference you use for the accelerometer would solve the problem... IE, when the callback is triggered, you STOP the accelerometer action, compute results, and then restart the accelerometer.. I just tried that - it made the application lock up, leaving the sensors unusable until next reboot of the phone. I don't think these start/stop calls are supposed to be called as often as it does the callback... -- http://mail.python.org/mailman/listinfo/python-list
Re: Protecting against callbacks queuing up?
On Monday 24 August 2009 17:32:23 Esben von Buchwald wrote: Hendrik van Rooyen wrote: 8 -- some stuff about an after call -- I'm new to python, what is an after function and an after call? Couldn't find excact answer on google...? Do you have a link to some docs? The after call is common in GUI stuff. Here is a link to nice Tkinter manual - it is the first link if you google for: tkinter new mexico tech shipman www.nmt.edu/tcc/help/pubs/tkinter/ What after does is that it is like a normal call, except that it is not done immediately, but after some settable time, hence the after. - Hendrik -- http://mail.python.org/mailman/listinfo/python-list
Re: Protecting against callbacks queuing up?
Dennis Lee Bieber wrote: On Mon, 24 Aug 2009 17:32:23 +0200, Esben von Buchwald find@paa.google declaimed the following in gmane.comp.python.general: I'm new to python, what is an after function and an after call? Couldn't find excact answer on google...? Do you have a link to some docs? They would be particular to whatever GUI/event library is in use. They aren't part of Python itself. This is how the accelerometer is accessed http://pys60.garage.maemo.org/doc/s60/node59.html I found this called after... http://pys60.garage.maemo.org/doc/s60/node12.html would that be usable? If so, how? -- http://mail.python.org/mailman/listinfo/python-list
Re: Protecting against callbacks queuing up?
On Tuesday 25 August 2009 15:21:16 Esben von Buchwald wrote: Dennis Lee Bieber wrote: On Mon, 24 Aug 2009 17:32:23 +0200, Esben von Buchwald find@paa.google declaimed the following in gmane.comp.python.general: I'm new to python, what is an after function and an after call? Couldn't find excact answer on google...? Do you have a link to some docs? They would be particular to whatever GUI/event library is in use. They aren't part of Python itself. This is how the accelerometer is accessed http://pys60.garage.maemo.org/doc/s60/node59.html I found this called after... http://pys60.garage.maemo.org/doc/s60/node12.html would that be usable? Probably If so, how? This is a guess, for your device, but I suspect something along these lines: t = Ao_timer() cb = t.after(100,thing_that_does_the_work(with_its_arguments)) Lots of assumptions here - the 100 should give you a tenth of a second... Don't know what the arguments look like, and if you need to pass an instance (like self) - that would depend on where you are calling it from. Play and see :-) You should also be able to cancel the callback like this: t.cancel(cb) If you do it before the time out - Hendrik -- http://mail.python.org/mailman/listinfo/python-list
Re: Protecting against callbacks queuing up?
On Monday 24 August 2009 02:14:24 Esben von Buchwald wrote: Hello I'm using Python for S60 1.9.7 on my Nokia phone. I've made a program that gets input from an accelerometer sensor, and then calculates some stuff and displays the result. The sensor framework API does a callback to a function defined by me, every time new data is available. The problem is, that sometimes, the calculations may take longer than others, but if the callback is called before the first calculation is done, it seems like it's queuing up all the callbacks, and execute them sequentially, afterwards. What I need, is to simply ignore the callback, if the previous call hasn't finished it's operations before it's called again. I thought that this code would do the trick, but it obviously doesn't help at all, and i can't understand why... def doCallback(self): if self.process_busy==False: self.process_busy=True self.data_callback() self.process_busy=False see if there is an after method somewhere. What you have to do is to break the link between the callback and the processing. Your code above is all in the callback thread, despite the fact that you call another function to do the processing. So if you replace the data_callback() with an after call, it should work as you expect. HTH - Hendrik -- http://mail.python.org/mailman/listinfo/python-list
Re: Protecting against callbacks queuing up?
Hendrik van Rooyen wrote: see if there is an after method somewhere. What you have to do is to break the link between the callback and the processing. Your code above is all in the callback thread, despite the fact that you call another function to do the processing. So if you replace the data_callback() with an after call, it should work as you expect. HTH - Hendrik Thanks I'm new to python, what is an after function and an after call? Couldn't find excact answer on google...? Do you have a link to some docs? -- http://mail.python.org/mailman/listinfo/python-list
Re: Protecting against callbacks queuing up?
On Aug 23, 8:14 pm, Esben von Buchwald find@paa.google wrote: I thought that this code would do the trick, but it obviously doesn't help at all, and i can't understand why... def doCallback(self): if self.process_busy==False: self.process_busy=True self.data_callback() self.process_busy=False doCallback is defined as a callback function for the accelerometer instance, and data_callback does some calculations and show them on the display of the phone. What to do? Thanks... As Dennis pointed out, it sounds like doCallback() is called serially. That is, you can think of doCallback() as being called in a loop, rather than entered multiple times simultaneously. Your 'busy' flag is never actually checked until after it has already been reset to False. If the data accessed by the callback as a unique copy and has a timestamp then a simple approach which avoids multithreading would be to have your callback ensure that any 'old' data is simply ignored. This will allow it to quickly catch up to a 'new' event. -- http://mail.python.org/mailman/listinfo/python-list
Protecting against callbacks queuing up?
Hello I'm using Python for S60 1.9.7 on my Nokia phone. I've made a program that gets input from an accelerometer sensor, and then calculates some stuff and displays the result. The sensor framework API does a callback to a function defined by me, every time new data is available. The problem is, that sometimes, the calculations may take longer than others, but if the callback is called before the first calculation is done, it seems like it's queuing up all the callbacks, and execute them sequentially, afterwards. What I need, is to simply ignore the callback, if the previous call hasn't finished it's operations before it's called again. I thought that this code would do the trick, but it obviously doesn't help at all, and i can't understand why... def doCallback(self): if self.process_busy==False: self.process_busy=True self.data_callback() self.process_busy=False doCallback is defined as a callback function for the accelerometer instance, and data_callback does some calculations and show them on the display of the phone. What to do? Thanks... -- http://mail.python.org/mailman/listinfo/python-list
Re: Protecting against callbacks queuing up?
Esben von Buchwald wrote: Hello I'm using Python for S60 1.9.7 on my Nokia phone. I've made a program that gets input from an accelerometer sensor, and then calculates some stuff and displays the result. The sensor framework API does a callback to a function defined by me, every time new data is available. The problem is, that sometimes, the calculations may take longer than others, but if the callback is called before the first calculation is done, it seems like it's queuing up all the callbacks, and execute them sequentially, afterwards. What I need, is to simply ignore the callback, if the previous call hasn't finished it's operations before it's called again. I thought that this code would do the trick, but it obviously doesn't help at all, and i can't understand why... def doCallback(self): if self.process_busy==False: self.process_busy=True self.data_callback() self.process_busy=False doCallback is defined as a callback function for the accelerometer instance, and data_callback does some calculations and show them on the display of the phone. If it is, in fact, queuing the callbacks then that means that it's just recording that the callback needs to be called, but doesn't actually do it while a previous one is in effect, ie it'll wait for the current call to return before doing the next one. What to do? Thanks... If you want to try multithreading you could make the callback just trigger the actual action in another thread. The other thread could wait for an event, perform the calculation, then clear any pending events before waiting again. -- http://mail.python.org/mailman/listinfo/python-list