Created
June 13, 2012 20:38
-
-
Save pcperini/2926385 to your computer and use it in GitHub Desktop.
Run Methods Asynchronously with a Simple Decorator
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
import threading | |
def _async(func, *a, **kw): | |
thread = threading.Thread(target = func, args = a, kwargs = kw) | |
thread.start() | |
return thread | |
def async(func): | |
return lambda *a, **kw: _async(func, *a, **kw) |
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
import time | |
from async import async | |
@async | |
def async_example(arg): | |
time.sleep(5) | |
print arg | |
def example(): | |
async_example("world") | |
print "hello" | |
# >>> hello | |
# >>> world |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment