### Install QuasiQueue Source: https://github.com/tedivm/quasiqueue/blob/main/README.md Provides the command to install the QuasiQueue library using pip. ```bash pip install quasiqueue ``` -------------------------------- ### Python QuasiQueue Basic Example Source: https://github.com/tedivm/quasiqueue/blob/main/README.md Demonstrates the basic usage of QuasiQueue by defining asynchronous writer and reader functions and initializing the QuasiQueue with a name and these functions. The example shows how to run the queue using asyncio. ```python import asyncio from quasiqueue import QuasiQueue async def writer(desired: int): """Feeds data to the Queue when it is low. """ for x in range(0, desired): yield x async def reader(identifier: int|str): """Receives individual items from the queue. Args: identifier (int | str): Comes from the output of the Writer function """ print(f"{identifier}") runner = QuasiQueue( "hello_world", reader=reader, writer=writer, ) if __name__ == '__main__': asyncio.run(runner.main()) ``` -------------------------------- ### Python Reader Function Example Source: https://github.com/tedivm/quasiqueue/blob/main/README.md Illustrates a simple asynchronous reader function for QuasiQueue, showing how it receives an identifier from the writer and prints it. This function can be synchronous or asynchronous. ```python async def reader(identifier: int|str): """Receives individual items from the queue. Args: identifier (int | str): Comes from the output of the Writer function """ print(f"{identifier}") ``` -------------------------------- ### Initialize QuasiQueue with Overridden Settings Source: https://github.com/tedivm/quasiqueue/blob/main/README.md Demonstrates initializing QuasiQueue with a Settings object, overriding default values like lookup_block_size. Note that environment variable prefixes are lost when using this method directly. ```python from quasiqueue import Settings, QuasiQueue QuasiQueue( "MyQueue", reader=reader, writer=writer, settings=Settings(lookup_block_size=50) ) ``` -------------------------------- ### Extend Settings Class with Custom Prefix Source: https://github.com/tedivm/quasiqueue/blob/main/README.md Shows how to extend the base Settings class to define a custom environment variable prefix (e.g., MY_QUEUE_) and add custom fields. This allows for more organized configuration management. ```python from quasiqueue import Settings, QuasiQueue from pydantic_settings import SettingsConfigDict class MySettings(Settings) model_config = SettingsConfigDict(env_prefix="MY_QUEUE_") lookup_block_size: int = 50 QuasiQueue( "MyQueue", reader=reader, writer=writer, settings=MySettings() ) ``` -------------------------------- ### QuasiQueue Context and Reader Functions Source: https://github.com/tedivm/quasiqueue/blob/main/README.md The optional context function runs once to initialize resources like database or HTTP connection pools. It should return a dictionary, which is then passed to the reader function. The reader function processes individual items from the queue, utilizing the provided context for operations. ```Python def context(): ctx = {} ctx['http'] = get_http_connection_pool() ctx['dbengine'] = get_db_engine_pool() return ctx def reader(identifier: int|str, ctx: Dict[str, Any]): """Receives individual items from the queue. Args: identifier (int | str): Comes from the output of the Writer function ctx (Dict[str, Any]): Comes from the output of the Context function """ ctx['dbengine'].execute("get item") ctx['http'].get("url") print(f"{identifier}") runner = QuasiQueue( "hello_world", reader=reader, writer=writer, context=context ) ``` -------------------------------- ### Access QuasiQueue Settings in Reader Function Source: https://github.com/tedivm/quasiqueue/blob/main/README.md Illustrates how to access the QuasiQueue settings within a reader function by defining a 'settings' argument. This allows functions to utilize configuration parameters like 'project_name'. ```python async def reader(item: Any, settings: Dict[str, Any]) print(settings['project_name']) ``` -------------------------------- ### QuasiQueue Writer Function Source: https://github.com/tedivm/quasiqueue/blob/main/README.md The writer function is responsible for feeding data to the QuasiQueue when the queue is low. It should return an iterator of picklable items. Generators are recommended for memory efficiency. The function is asynchronous and accepts the desired number of items as an argument, which can be used for optimization. ```Python async def writer(desired: int): """Feeds data to the Queue when it is low. """ return range(0, desired) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.