Created
April 5, 2020 16:09
-
-
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?
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 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