Last active
August 31, 2015 20:09
-
-
Save tito/d4b726fc3c9c1d580851 to your computer and use it in GitHub Desktop.
Threaded decorator for kivy with callback
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
from kivy.clock import mainthread | |
def threaded(callback): | |
def _threaded(f): | |
def _threaded_callback(*args, **kwargs): | |
def _run_in_threaded_callback(*args, **kwargs): | |
ret = f(*args, **kwargs) | |
mainthread(callback)(ret) | |
thread = threading.Thread(target=_run_in_threaded_callbacks, args=args, kwargs=kwargs) | |
thread.daemon = True | |
thread.start() | |
return _threaded_callback | |
return _threaded | |
# example | |
def on_compute_done(result): | |
print result | |
@threaded(on_compute_done) | |
def compute(): | |
import time | |
time.sleep(5) | |
return "hello" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment