Repository navigation
gh-97696: DRAFT asyncio eager tasks factory prototype #101613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
1ea845c
Copy GH-98137, make some changes, add benchmarking script, trying to …
itamaro de6d910
asyncio runner takes optional task factory arg
itamaro 56610e7
don't coro.send if the event loop is not running (yet)
itamaro 34082a7
modify async tree script to support eager factory
itamaro ce5beaf
don't over-count tasks that yield result immediately
itamaro 38d7b0b
handle task._source_traceback in eager task factory
itamaro 6afbaab
stop checking for eager factory in taskgroups
itamaro a6e68bc
refactor async tree benchmark to work with TaskGroup or gather depend…
itamaro 464bd49
restore C task
itamaro ac26ad6
yield_result -> coro_result
itamaro ae2dcf3
Support `coro_result` in Task C impl
itamaro 9794715
Merge branch 'main' into asyncio-eager-tasks-playground
itamaro 0b447b0
Address Dino's review comments
itamaro f2748e2
passthrough coro_result from custom constructor only if it is set
itamaro 61ac5d0
add != NULL assertion on step2 result
itamaro cbd14fb
Merge branch 'main' into asyncio-eager-tasks-playground
itamaro 3503337
fix result refcnting in task_step_handle_result_impl
itamaro 49c0e89
Add eager task factory tests
itamaro 8a5229a
cleanup eager task factory tests
itamaro d1e5fd1
add news blurb
itamaro 580fb1f
Merge branch 'main' into asyncio-eager-tasks-playground
itamaro 6284c41
apply patchcheck fixes in asyncio.tasks
itamaro 34123a7
regenerate clinic
itamaro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Copy GH-98137, make some changes, add benchmarking script, trying to …
…get it working
- Loading branch information
commit 1ea845c1e79f67a4c95a80439bb82105755c3e93
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| # Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com) | ||
| """ | ||
| Benchmark script for recursive async tree workloads. This script includes the | ||
| following microbenchmark scenarios: | ||
|
|
||
| 1) "no_suspension": No suspension in the async tree. | ||
| 2) "suspense_all": Suspension (simulating IO) at all leaf nodes in the async tree. | ||
| 3) "memoization": Simulated IO calls at all leaf nodes, but with memoization. Only | ||
| un-memoized IO calls will result in suspensions. | ||
| 4) "cpu_io_mixed": A mix of CPU-bound workload and IO-bound workload (with | ||
| memoization) at the leaf nodes. | ||
|
|
||
| Use the commandline flag or choose the corresponding <Scenario>AsyncTree class | ||
| to run the desired microbenchmark scenario. | ||
| """ | ||
|
|
||
|
|
||
| import asyncio | ||
| import math | ||
| import random | ||
| import time | ||
| from argparse import ArgumentParser | ||
|
|
||
|
|
||
| NUM_RECURSE_LEVELS = 6 | ||
| NUM_RECURSE_BRANCHES = 6 | ||
| IO_SLEEP_TIME = 0.05 | ||
| DEFAULT_MEMOIZABLE_PERCENTAGE = 90 | ||
| DEFAULT_CPU_PROBABILITY = 0.5 | ||
| FACTORIAL_N = 500 | ||
|
|
||
|
|
||
| def parse_args(): | ||
| parser = ArgumentParser( | ||
| description="""\ | ||
| Benchmark script for recursive async tree workloads. It can be run as a standalone | ||
| script, in which case you can specify the microbenchmark scenario to run and whether | ||
| to print the results. | ||
| """ | ||
| ) | ||
| parser.add_argument( | ||
| "-s", | ||
| "--scenario", | ||
| choices=["no_suspension", "suspense_all", "memoization", "cpu_io_mixed"], | ||
| default="no_suspension", | ||
| help="""\ | ||
| Determines which microbenchmark scenario to run. Defaults to no_suspension. Options: | ||
| 1) "no_suspension": No suspension in the async tree. | ||
| 2) "suspense_all": Suspension (simulating IO) at all leaf nodes in the async tree. | ||
| 3) "memoization": Simulated IO calls at all leaf nodes, but with memoization. Only | ||
| un-memoized IO calls will result in suspensions. | ||
| 4) "cpu_io_mixed": A mix of CPU-bound workload and IO-bound workload (with | ||
| memoization) at the leaf nodes. | ||
| """, | ||
| ) | ||
| parser.add_argument( | ||
| "-m", | ||
| "--memoizable-percentage", | ||
| type=int, | ||
| default=DEFAULT_MEMOIZABLE_PERCENTAGE, | ||
| help="""\ | ||
| Sets the percentage (0-100) of the data that should be memoized, defaults to 90. For | ||
| example, at the default 90 percent, data 1-90 will be memoized and data 91-100 will not. | ||
| """, | ||
| ) | ||
| parser.add_argument( | ||
| "-c", | ||
| "--cpu-probability", | ||
| type=float, | ||
| default=DEFAULT_CPU_PROBABILITY, | ||
| help="""\ | ||
| Sets the probability (0-1) that a leaf node will execute a cpu-bound workload instead | ||
| of an io-bound workload. Defaults to 0.5. Only applies to the "cpu_io_mixed" | ||
| microbenchmark scenario. | ||
| """, | ||
| ) | ||
| parser.add_argument( | ||
| "-p", | ||
| "--print", | ||
| action="store_true", | ||
| default=False, | ||
| help="Print the results (runtime and number of Tasks created).", | ||
| ) | ||
| return parser.parse_args() | ||
|
|
||
|
|
||
| class AsyncTree: | ||
| def __init__( | ||
| self, | ||
| memoizable_percentage=DEFAULT_MEMOIZABLE_PERCENTAGE, | ||
| cpu_probability=DEFAULT_CPU_PROBABILITY, | ||
| ): | ||
| self.suspense_count = 0 | ||
| self.task_count = 0 | ||
| self.memoizable_percentage = memoizable_percentage | ||
| self.cpu_probability = cpu_probability | ||
| self.cache = {} | ||
| # set to deterministic random, so that the results are reproducible | ||
| random.seed(0) | ||
|
|
||
| async def mock_io_call(self): | ||
| self.suspense_count += 1 | ||
| await asyncio.sleep(IO_SLEEP_TIME) | ||
|
|
||
| def create_task(self, loop, coro): | ||
| self.task_count += 1 | ||
| return asyncio.Task(coro, loop=loop) | ||
|
|
||
| async def suspense_func(self): | ||
| raise NotImplementedError( | ||
| "To be implemented by each microbenchmark's derived class." | ||
| ) | ||
|
|
||
| async def recurse(self, recurse_level): | ||
| if recurse_level == 0: | ||
| await self.suspense_func() | ||
| return | ||
|
|
||
| await asyncio.gather( | ||
| *[self.recurse(recurse_level - 1) for _ in range(NUM_RECURSE_BRANCHES)] | ||
| ) | ||
|
|
||
| def run(self): | ||
| loop = asyncio.new_event_loop() | ||
| # eager_factory = asyncio.create_eager_task_factory(self.create_task) | ||
| # loop.set_task_factory(eager_factory) | ||
| loop.set_task_factory(asyncio.eager_task_factory) | ||
| loop.run_until_complete(self.recurse(NUM_RECURSE_LEVELS)) | ||
| loop.close() | ||
|
|
||
|
|
||
| class NoSuspensionAsyncTree(AsyncTree): | ||
| async def suspense_func(self): | ||
| return | ||
|
|
||
|
|
||
| class SuspenseAllAsyncTree(AsyncTree): | ||
| async def suspense_func(self): | ||
| await self.mock_io_call() | ||
|
|
||
|
|
||
| class MemoizationAsyncTree(AsyncTree): | ||
| async def suspense_func(self): | ||
| # deterministic random (seed preset) | ||
| data = random.randint(1, 100) | ||
|
|
||
| if data <= self.memoizable_percentage: | ||
| if self.cache.get(data): | ||
| return data | ||
|
|
||
| self.cache[data] = True | ||
|
|
||
| await self.mock_io_call() | ||
| return data | ||
|
|
||
|
|
||
| class CpuIoMixedAsyncTree(MemoizationAsyncTree): | ||
| async def suspense_func(self): | ||
| if random.random() < self.cpu_probability: | ||
| # mock cpu-bound call | ||
| return math.factorial(FACTORIAL_N) | ||
| else: | ||
| return await MemoizationAsyncTree.suspense_func(self) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| args = parse_args() | ||
| scenario = args.scenario | ||
|
|
||
| trees = { | ||
| "no_suspension": NoSuspensionAsyncTree, | ||
| "suspense_all": SuspenseAllAsyncTree, | ||
| "memoization": MemoizationAsyncTree, | ||
| "cpu_io_mixed": CpuIoMixedAsyncTree, | ||
| } | ||
| async_tree_class = trees[scenario] | ||
| async_tree = async_tree_class(args.memoizable_percentage, args.cpu_probability) | ||
|
|
||
| start_time = time.perf_counter() | ||
| async_tree.run() | ||
| end_time = time.perf_counter() | ||
|
|
||
| if args.print: | ||
| print(f"Scenario: {scenario}") | ||
| print(f"Time: {end_time - start_time} s") | ||
| print(f"Tasks created: {async_tree.task_count}") | ||
| print(f"Suspense called: {async_tree.suspense_count}") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.