Skip to content

Instantly share code, notes, and snippets.

@Bulletninja
Created October 25, 2016 01:51
Show Gist options
  • Save Bulletninja/7521c889a847098944d7faf25f259232 to your computer and use it in GitHub Desktop.
Save Bulletninja/7521c889a847098944d7faf25f259232 to your computer and use it in GitHub Desktop.
def run_async(f, lock=None):
import weakref
from threading import Thread
def args_wrapper(*args, **kwargs):
class FunctionRunner(Thread):
def run(self):
if lock is not None: lock.acquire()
try:
self.result = f(*args, **kwargs)
finally:
if lock is not None: lock.release()
t = FunctionRunner()
t.start()
def callback():
t.join()
return t.result
proxy = weakref.proxy(callback)
return proxy
return args_wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment