Skip to content

Instantly share code, notes, and snippets.

@betatim
Created April 5, 2020 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save betatim/1c2c279b466effb5ec40169f76d89762 to your computer and use it in GitHub Desktop.
Save betatim/1c2c279b466effb5ec40169f76d89762 to your computer and use it in GitHub Desktop.
How to call async code from sync code that was called from async code?
import asyncio
async def asyncs():
asyncio.sleep(1)
return 6
def times_syncs(x):
# I would like to be able to do call `asyncs()` somehow, but can't
# figure out how to do that
# return x * await asyncs()
return x * 6
async def async_outer():
return times_syncs(7)
def main():
x = asyncio.get_event_loop().run_until_complete(
async_outer()
)
print("What is six times seven?", x)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment