Created
October 25, 2016 01:51
-
-
Save Bulletninja/7521c889a847098944d7faf25f259232 to your computer and use it in GitHub Desktop.
This file contains 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
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