-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat($asyncio): create demo for
asyncio
- Loading branch information
1 parent
ba1d64e
commit b6a426e
Showing
10 changed files
with
257 additions
and
49 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import asyncio | ||
from typing import Any | ||
|
||
from loguru import logger | ||
|
||
from python_boilerplate.__main__ import startup | ||
from python_boilerplate.common.asynchronization import async_function_wrapper | ||
from python_boilerplate.common.profiling import async_elapsed_time | ||
|
||
|
||
@async_elapsed_time() | ||
@async_function_wrapper | ||
async def coroutine1() -> int: | ||
logger.info("Coroutine 1 starting...") | ||
await asyncio.sleep(1) | ||
logger.info("Coroutine 1 finished!") | ||
return 42 | ||
|
||
|
||
@async_elapsed_time() | ||
@async_function_wrapper | ||
async def coroutine2() -> str: | ||
logger.info("Coroutine 2 starting...") | ||
await asyncio.sleep(2) | ||
logger.info("Coroutine 2 finished!") | ||
return "Hello, world!" | ||
|
||
|
||
@async_elapsed_time() | ||
@async_function_wrapper | ||
async def coroutine3() -> None: | ||
logger.info("Coroutine 3 starting...") | ||
await asyncio.sleep(1) | ||
raise ValueError("Something went wrong") | ||
|
||
|
||
async def main() -> None: | ||
# Run both coroutines concurrently using asyncio.gather() | ||
results: list[Any] = await asyncio.gather( | ||
*[coroutine1(), coroutine2(), coroutine3()], return_exceptions=True | ||
) | ||
logger.info(f"Results: {results}") | ||
|
||
|
||
if __name__ == "__main__": | ||
startup() | ||
# Run the event loop | ||
asyncio.run(main()) | ||
logger.info(type(coroutine3)) |
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
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
Oops, something went wrong.