Skip to content

Instantly share code, notes, and snippets.

@tito
Last active August 31, 2015 20:09
Show Gist options
  • Save tito/d4b726fc3c9c1d580851 to your computer and use it in GitHub Desktop.
Save tito/d4b726fc3c9c1d580851 to your computer and use it in GitHub Desktop.
Threaded decorator for kivy with callback
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