Created
November 3, 2023 18:36
-
-
Save bryaneaton/b4adb1d023c001a73c6319a3a70757d6 to your computer and use it in GitHub Desktop.
Asyncio and Click
This file contains hidden or 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 operation definition remains the same | |
async def async_operation(): | |
print("Starting async operation") | |
await asyncio.sleep(1) | |
print("Async operation completed") | |
# This is the main coroutine that will gather and run the async tasks | |
async def run_multiple_async_operations(count): | |
tasks = [async_operation() for _ in range(count)] | |
await asyncio.gather(*tasks) # Use gather to run tasks concurrently | |
# The click command calls the coroutine using asyncio.run | |
@actions.command(name="run_async") | |
@click.option('--count', default=1, help='Number of times to run the async operation.') | |
def run_async(count): | |
asyncio.run(run_multiple_async_operations(count)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment