Created
April 5, 2022 20:28
-
-
Save realFranco/79002d1ff51c7b6635fef6c984bcef13 to your computer and use it in GitHub Desktop.
Dont wait for async functions, create tasks and schedule those computations on the same event loop #python #python3 #asyncio #async #io #task #facyt
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
# Franco Gil | |
# @realFranco | |
# April 5th, 2022 | |
class Utils: | |
@staticmethod | |
def noBlockCoroutine(coroutine): | |
try: | |
# Wrap the coro coroutine into a Task and schedule its execution. | |
# Return the Task object. | |
asyncio.create_task(coroutine()) | |
return True | |
except Exception as err: | |
print(f'error - At no block corutine: {str(err)}\n') | |
return False | |
async def some_awaitable_computation(args: str): | |
# await computation or some i/o | |
return 1 + 1 | |
coroutine = some_awaitable_computation(args='CS') | |
Utils.noBlockCoroutine(coroutine=coroutine) | |
# continue the workflow | |
# "some_awaitable_computation" will be scheduled on the same event loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment