Skip to content

Instantly share code, notes, and snippets.

@lmazuel
Last active January 4, 2018 03:57
Show Gist options
  • Save lmazuel/f93e650dc778b210d7a4210f17ecdf55 to your computer and use it in GitHub Desktop.
Save lmazuel/f93e650dc778b210d7a4210f17ecdf55 to your computer and use it in GitHub Desktop.
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"
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
def foo(url):
return "Url: "+url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment