Skip to content

Instantly share code, notes, and snippets.

@ttamg
Last active June 7, 2020 16:46
Show Gist options
  • Save ttamg/32bd451ed3e41e4552e93bf3172731fc to your computer and use it in GitHub Desktop.
Save ttamg/32bd451ed3e41e4552e93bf3172731fc to your computer and use it in GitHub Desktop.
Simple usage example for unsync
import time
from unsync import unsync
@unsync
def my_function():
""" A function which we want to use asynchronously """
time.sleep(1)
return "something fascinating"
# Calling the decorated function starts the coroutine running
thing = my_function()
# Note that it returns an unsync Unfuture object, NOT the return value (yet)
print(thing)
-> <unsync.unsync.Unfuture object at 0x106f87b90>
# To await the coroutine finishing and get the final return value,
# call the .result() method on this Unfuture object
print(thing.result())
-> something fascinating
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment