Clock.schedule_once vs @mainthread decorator

453 views
Skip to first unread message

kra...@gmail.com

unread,
Jan 27, 2016, 5:31:08 AM1/27/16
to Kivy users support

Hello,

It is clear that one should not call functions that manipulate the UI from a python thread but if I want to have some function with input arguments called from a python thread that affects the UI is it better to do it via :

   Clock.schedule_once(partial(some_callback, input_arg, 'some-key'))

or apply the @mainthread decorator to the some_callback function?

What would is the difference between the two?  Either seem like they would work but I am not clear on the differences.

thanks,

Mike



Alexander Taylor

unread,
Jan 27, 2016, 12:54:29 PM1/27/16
to Kivy users support
The mainthread decorator actually just calls Clock.schedule_once (plus takes care of the arguments), it's just for convenience. You can use whichever method you find best.

Michael Kramer

unread,
Jan 29, 2016, 7:55:45 AM1/29/16
to Kivy users support
Many thanks for the explanation - that helps quite a bit.

On a related note are there ever any timing problems with calling methods with the @mainthread (or Clock.schedule_once) operations?

The reason I am asking is that I have found that if I call a method A with the @mainthread decorator before starting a Python thread, then call another @mainthread decorated method B once within the Python thread after it has been started  then, on a rare occasion, method B is run before method A even though the calling order is in the opposite. 

Is the scheduling on the Kivy UI thread guaranteed to force these to be in sequential order as one would expect in the code or could there be a sort of race condition where method B is scheduled before method A?  If I put enough of a time.sleep() at the start of the Python thread before the call to method B then these calls are separated enough in time that it is not a problem but this seems a bit hacky to me.

Would you happen to know if I were to use Clock.schedule_once with the argument to force to the call to method A to be operated before the next frame and then method B called to operate in the next frame (w/ -1 and 0 respectively) then would this force the order to be as expected?  Or could it be ensured by using the max_iteration property?

thanks again for your assistance,

Mike


--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages