### Install Asyncer Source: https://github.com/fastapi/asyncer/blob/main/docs/index.md Install Asyncer and its dependency AnyIO using pip. This command also shows the expected output upon successful installation. ```console $ pip install asyncer ---> 100% Successfully installed asyncer anyio ``` -------------------------------- ### Install Asyncer with Pip Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/install.md Use pip to install the Asyncer package. Ensure you have a virtual environment activated. ```console pip install asyncer ---> 100% Successfully installed asyncer anyio ``` -------------------------------- ### Running Both Async and Sync Programs Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/syncify-no-raise.md This example demonstrates running both an async program using `anyio.run()` and a sync program by directly calling a sync function. The `do_sync_work` function uses `syncify` to handle calls from both async and sync contexts. ```Python import anyio from asyncer import asyncify, syncify def do_sync_work(): async def do_async_work(): return 123 return syncify(do_async_work, raise_sync_error=False) async def main(): result = do_sync_work() print(f"Async result: {result}") def sync_main(): result = do_sync_work() print(f"Sync result: {result}") if __name__ == "__main__": print("Running async part...") anyio.run(main) print("\nRunning sync part...") sync_main() ``` -------------------------------- ### Full Example: Concurrent Execution Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/soonify.md A complete example demonstrating concurrent execution of multiple `do_work` calls using Asyncer's task group and `soonify`. This allows tasks to run in parallel, significantly reducing total execution time. ```python import asyncer import anyio async def do_work(name: str): await anyio.sleep(1) print(f"Hello, {name}") async def main(): async with asyncer.create_task_group() as task_group: task_group.soonify(do_work)(name="Yury") task_group.soonify(do_work)(name="Nathaniel") task_group.soonify(do_work)(name="Alex") anyio.run(main) ``` -------------------------------- ### Combine Async Functions and Run Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/first-steps.md This example demonstrates calling an async function `do_work` from within the main async function `main` and then running `main` using `anyio.run()`. ```python import anyio async def do_work(): await anyio.sleep(1) print("Working...") async def main(): await do_work() print("Hello, World") anyio.run(main) ``` -------------------------------- ### Console Output Example Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/first-steps.md This is an example of the expected console output when running the asynchronous program. It shows a delay before the final message is printed. ```console $ python main.py // Wait for it... // After around one second Hello, World ``` -------------------------------- ### Using syncify with raise_sync_error=False Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/syncify-no-raise.md When calling `syncify()` from a mainly sync program, set `raise_sync_error=False`. This prevents an error and runs the async code, potentially starting a new AnyIO event loop if none is active. ```Python import anyio from asyncer import syncify def do_sync_work(): async def do_async_work(): return 123 return syncify(do_async_work, raise_sync_error=False) result = do_sync_work() print(f"Result: {result}") ``` -------------------------------- ### Async Main Function with Arguments Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/runnify.md Define an async main function that accepts arguments. This sets up the function to be called later with specific parameters. ```python import asyncer import anyio async def main(name: str = "World"): await anyio.sleep(1) print(f"Hello, {name}") ``` -------------------------------- ### Run an Async Main Function with AnyIO Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/first-steps.md Use `anyio.run()` to execute your top-level async function. This function handles the event loop and awaits the completion of the provided async function. ```python import anyio async def main(): print("Hello, World") anyio.run(main) ``` -------------------------------- ### Call Sync Code from Async Code with asyncify Source: https://github.com/fastapi/asyncer/blob/main/docs/index.md Demonstrates how to use the `asyncify` function to run a synchronous function (`do_sync_work`) from an asynchronous context (`main`). The synchronous function is executed in a worker thread to avoid blocking the event loop. ```Python import time import anyio from asyncer import asyncify def do_sync_work(name: str): time.sleep(1) return f"Hello, {name}" async def main(): message = await asyncify(do_sync_work)(name="World") print(message) anyio.run(main) ``` -------------------------------- ### Run Async Function with Arguments using runnify Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/runnify.md Use `asyncer.runnify` to create a callable that wraps an async function, allowing you to pass arguments when executing it. This leverages `anyio.run` internally. ```python run_main = asyncer.runnify(main) run_main(name="World") ``` -------------------------------- ### Create Task Group with Asyncer Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/soonify.md Initialize a task group using `asyncer.create_task_group()` within an `async with` block. This group will manage the concurrent execution of tasks. ```python import asyncer async def main(): async with asyncer.create_task_group() as task_group: # Add tasks here ``` -------------------------------- ### Asyncify with Arguments Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/asyncify.md Illustrates how asyncify() handles synchronous functions that require arguments. The async wrapper function accepts the same arguments as the original sync function. ```python import time from asyncer import asyncify def sync_function_with_args(name: str, delay: int): time.sleep(delay) return f"Hello {name}!" async def main(): async_sync_function_with_args = asyncify(sync_function_with_args) result = await async_sync_function_with_args("World", 2) print(result) ``` -------------------------------- ### Soonify Multiple Functions with Asyncer Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/soonify.md Schedule multiple async functions to run concurrently by calling `task_group.soonify()` for each. The `async with` block ensures all tasks are awaited upon exit. ```python async with asyncer.create_task_group() as task_group: task_group.soonify(do_work)(name="Yury") task_group.soonify(do_work)(name="Nathaniel") task_group.soonify(do_work)(name="Alex") ``` -------------------------------- ### Console Output of Concurrent Execution Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/soonify.md Illustrates the expected output when running the concurrent code. Notice the delay before all messages appear, indicating parallel execution. ```console $ python main.py // Enjoy the silence... // All the output at once! 🎉 Hello, Yury Hello, Nathaniel Hello, Alex ``` -------------------------------- ### Use Asyncify to Call Sync Code Safely Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/asyncify.md Shows how to use Asyncer's asyncify() to wrap a synchronous blocking function. The returned async function can be awaited without blocking the event loop, as the original sync function will run in a worker thread. ```python import time from asyncer import asyncify def do_sync_work(): time.sleep(1) return "Sync work done!" async def main(): async_do_sync_work = asyncify(do_sync_work) result = await async_do_sync_work() print(result) ``` -------------------------------- ### Check SoonValue.ready Attribute Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/soonify-return.md Use the `.ready` attribute to check if the value of a SoonValue object is available. This is useful inside async contexts where pending async code might have already computed the value. ```python async with anyio.open_cancel_scope() as scope: scope.move_on_after(2) await soon_value1 assert soon_value1.ready print(f"The value is: {soon_value1.value}") ``` -------------------------------- ### Define an Async Main Function Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/first-steps.md Declare an async function using `async def`. You can use `await` inside these functions to pause execution until an awaitable completes. ```python async def main(): print("Hello, World") ``` -------------------------------- ### Soonify One Function with Asyncer Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/soonify.md Use `task_group.soonify()` to schedule an async function for concurrent execution. It returns a function that accepts the arguments for the target async function. ```python async with asyncer.create_task_group() as task_group: task_group.soonify(do_work)(name="Yury") ``` -------------------------------- ### Define an Async Function for Work Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/first-steps.md Define functions that perform I/O bound operations or need to pause execution using `async def`. This allows them to be `await`ed. ```python async def do_work(): await anyio.sleep(1) print("Working...") ``` -------------------------------- ### Async function not returning a value Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/soonify-return.md This async function takes a parameter and prints a message but does not explicitly return anything, effectively returning None. ```python async def do_work(name): print(f"Doing work for {name}") ``` -------------------------------- ### Call Sync Code Directly from Async Code (Blocking) Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/asyncify.md Demonstrates the incorrect way to call a blocking synchronous function from an async function. This will block the event loop, preventing other async tasks from running. ```python import asyncio async def main(): result = do_sync_work() # This blocks the event loop print(result) ``` -------------------------------- ### Async Code - Not Concurrent Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/soonify.md Calling async functions sequentially without a mechanism for concurrent execution. Python waits for each `await` to complete before proceeding to the next call. ```python async def get_data(): await do_work(name="Yury") await do_work(name="Nathaniel") await do_work(name="Alex") ``` -------------------------------- ### Call Async Function from Sync Code with syncify() Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/syncify.md Use `syncify()` to call an async function from a sync function. This is useful when a sync function, potentially running in a worker thread via `asyncify()`, needs to perform an async operation. `syncify()` ensures the async operation is run and its result is returned compatibly. ```python import anyio from asyncer import syncify async def do_async_work(): return "Hello from async world!" def do_sync_work(): # Call the async function from sync code message = syncify(do_async_work)() # Note the extra () to call the syncified function print(message) def main(): # Run the sync function that calls async code syncify(do_sync_work)() if __name__ == "__main__": anyio.run(main) ``` -------------------------------- ### Storing SoonValue objects Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/soonify-return.md When using `task_group.soonify()`, the call returns a `SoonValue` object that can be stored in a variable to later access its result. ```python async with asyncer.create_task_group() as task_group: soon_value1 = task_group.soonify(do_work)("Alice") soon_value2 = task_group.soonify(do_work)("Bob") ``` -------------------------------- ### Define a Synchronous Blocking Function Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/asyncify.md This is a regular Python function that simulates blocking I/O operations using time.sleep(). It does not use 'await' and will block the event loop if called directly from async code. ```python import time def do_sync_work(): time.sleep(1) return "Sync work done!" ``` -------------------------------- ### Accessing SoonValue inside task group (raises PendingValueException) Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/soonify-return.md Attempting to access the `.value` attribute of a `SoonValue` object inside the `async with` block before the tasks have completed will raise a `PendingValueException`. ```python async with asyncer.create_task_group() as task_group: soon_value1 = task_group.soonify(do_work)("Alice") print(soon_value1.value) # This will raise PendingValueException ``` -------------------------------- ### Async function returning a string value Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/soonify-return.md This async function is modified to return a string value instead of printing a message, allowing the result to be used later. ```python async def do_work(name): return f"Work done for {name}" ``` -------------------------------- ### Accessing return value from SoonValue object Source: https://github.com/fastapi/asyncer/blob/main/docs/tutorial/soonify-return.md After the `async with` block, the `.value` attribute of a `SoonValue` object contains the return value of the completed async function. ```python print(soon_value1.value) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.