Last active
January 4, 2018 03:57
-
-
Save lmazuel/f93e650dc778b210d7a4210f17ecdf55 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 wrapper(url, awaitable=False): | |
"""This is wrapper, that return the result or a awaitable future to get the result. | |
This wrapper does not involve asyncio and can parsed by Python 2.7.""" | |
if awaitable: | |
from async_version import foo | |
return foo(url) | |
else: | |
from sync_version import foo | |
return foo(url) | |
# Not async, works | |
result = wrapper("urn:test") | |
assert result == "Url: urn:test" | |
# Async, works | |
import asyncio | |
loop = asyncio.get_event_loop() | |
result = loop.run_until_complete(wrapper("urn:test", awaitable=True)) | |
assert result == "Url: urn:test" |
This file contains hidden or 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 asyncio | |
async def _foo(future, url): | |
await asyncio.sleep(1) | |
future.set_result("Url: "+url) | |
def foo(url): | |
future = asyncio.Future() | |
asyncio.ensure_future(_foo(future, url)) | |
return future |
This file contains hidden or 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 foo(url): | |
return "Url: "+url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment