### Python: Basic Producer-Consumer Setup with Async-Channel Source: https://context7.com/drakkar-software/async-channel/llms.txt Demonstrates a basic producer-consumer system using Async-Channel. It shows how to define custom producer and consumer classes, set up a channel, register a consumer with a callback, and run the producer. This example highlights automatic message routing to registered consumers. ```python import asyncio import async_channel.consumer as consumer import async_channel.producer as producer import async_channel.channels as channels import async_channel.util as util # Define custom producer that generates data class AwesomeProducer(producer.Producer): async def start(self): # Background task - generates data continuously for i in range(5): await self.send({"value": i, "message": f"Data {i}"}) await asyncio.sleep(0.1) # Define consumer class class AwesomeConsumer(consumer.Consumer): pass # Define channel connecting producers and consumers class AwesomeChannel(channels.Channel): PRODUCER_CLASS = AwesomeProducer CONSUMER_CLASS = AwesomeConsumer async def callback(value, message): print(f"Consumer received: {message} with value {value}") async def main(): # Create and register the channel await util.create_channel_instance(AwesomeChannel, channels.set_chan) # Get channel instance and add a consumer channel = channels.get_chan("Awesome") await channel.new_consumer(callback) # Create and run producer prod = AwesomeProducer(channel) await prod.run() # Let it process await asyncio.sleep(1) # Cleanup await channel.stop() asyncio.run(main()) ``` -------------------------------- ### Async-Channel Usage Example Source: https://github.com/drakkar-software/async-channel/blob/master/README.md Demonstrates how to use the async-channel library to create producers, consumers, and channels for inter-task communication. It shows setting up custom producer and consumer classes, creating a channel, registering a callback, and sending data through the channel. ```python import async_channel.consumer as consumer import async_channel.producer as producer import async_channel.channels as channels import async_channel.util as util class AwesomeProducer(producer.Producer): pass class AwesomeConsumer(consumer.Consumer): pass class AwesomeChannel(channels.Channel): PRODUCER_CLASS = AwesomeProducer CONSUMER_CLASS = AwesomeConsumer async def callback(data): print("Consumer called !") print("Received : " + data) # Creates the channel await util.create_channel_instance(AwesomeChannel, channels.Channels) # Add a new consumer to the channel await channels.Channels.get_chan("Awesome").new_consumer(callback) # Creates a producer that send data to the consumer through the channel producer = AwesomeProducer(channels.Channels.get_chan("Awesome")) await producer.run() await producer.send("test") # Stops the channel with all its producers and consumers # await channels.Channels.get_chan("Awesome").stop() ``` -------------------------------- ### Manage Multiple Channels with IDs in Python Source: https://context7.com/drakkar-software/async-channel/llms.txt This example shows how to create and manage multiple channel instances with unique identifiers. It demonstrates creating channels with specific IDs, adding consumers to each, sending data, and retrieving channels by their IDs. It uses UUIDs for the channel IDs and shows how to create, get, and delete channels. ```python import asyncio import uuid import async_channel.channels as channels import async_channel.util as util class DataChannel(channels.Channel): def __init__(self, test_id): super().__init__() self.chan_id = test_id async def user_handler(**kwargs): print(f"User channel: {kwargs}") async def admin_handler(**kwargs): print(f"Admin channel: {kwargs}") async def main(): # Create separate channel instances with unique IDs user_channel_id = uuid.uuid4().hex admin_channel_id = uuid.uuid4().hex await util.create_channel_instance( DataChannel, channels.set_chan_at_id, channel_name="UserData", test_id=user_channel_id ) await util.create_channel_instance( DataChannel, channels.set_chan_at_id, channel_name="AdminData", test_id=admin_channel_id ) # Retrieve specific channels user_channel = channels.get_chan_at_id("UserData", user_channel_id) admin_channel = channels.get_chan_at_id("AdminData", admin_channel_id) # Add consumers to each channel await user_channel.new_consumer(user_handler) await admin_channel.new_consumer(admin_handler) # Send data to specific channels await user_channel.get_internal_producer().send({"user_id": 123, "action": "login"}) await admin_channel.get_internal_producer().send({"admin_id": 1, "action": "review"}) await asyncio.sleep(0.1) # Get all channels for a specific ID all_user_channels = channels.get_channels(user_channel_id) print(f"User channels: {list(all_user_channels.keys())}") # ['UserData'] # Cleanup await user_channel.stop() await admin_channel.stop() channels.del_chan_at_id("UserData", user_channel_id) channels.del_chan_at_id("AdminData", admin_channel_id) asyncio.run(main()) ``` -------------------------------- ### Python: Producer.send() with Async-Channel for Event Distribution Source: https://context7.com/drakkar-software/async-channel/llms.txt Illustrates the use of the Producer.send() method in Async-Channel to distribute data to multiple consumers. This example demonstrates sending different event payloads and shows how each event is received by all registered consumers, simulating an event broadcasting mechanism. ```python import asyncio import async_channel.channels as channels import async_channel.producer as producer import async_channel.util as util class DataProducer(producer.Producer): async def start(self): # Send data to all consumers await self.send({"event": "user_login", "user_id": 123}) await self.send({"event": "purchase", "amount": 99.99, "user_id": 123}) class DataChannel(channels.Channel): PRODUCER_CLASS = DataProducer async def event_handler(**kwargs): print(f"Event received: {kwargs}") async def main(): await util.create_channel_instance(DataChannel, channels.set_chan) channel = channels.get_chan("Data") # Add multiple consumers await channel.new_consumer(event_handler) await channel.new_consumer(event_handler) # Start producer prod = DataProducer(channel) await prod.run() await asyncio.sleep(0.1) await channel.stop() asyncio.run(main()) ``` -------------------------------- ### Channel Lifecycle Management in Python Source: https://context7.com/drakkar-software/async-channel/llms.txt Illustrates the complete lifecycle of an async channel, from creation to deletion. This includes adding and removing consumers, registering and managing producers, modifying channel behavior, and properly stopping and flushing channel resources. It emphasizes best practices for resource management in asynchronous applications. ```python import asyncio import async_channel.channels as channels import async_channel.producer as producer import async_channel.util as util class LifecycleProducer(producer.Producer): async def start(self): counter = 0 while not self.should_stop: await self.send({"count": counter}) counter += 1 await asyncio.sleep(0.1) class LifecycleChannel(channels.Channel): PRODUCER_CLASS = LifecycleProducer async def handler(**kwargs): print(f"Data: {kwargs}") async def main(): # Step 1: Create channel await util.create_channel_instance(LifecycleChannel, channels.set_chan) channel = channels.get_chan("Lifecycle") print("Channel created") # Step 2: Add consumers consumer1 = await channel.new_consumer(handler) consumer2 = await channel.new_consumer(handler) print(f"Consumers added: {len(channel.get_consumers())}") # Step 3: Create and register producer prod = LifecycleProducer(channel) await prod.run() print(f"Producers registered: {len(channel.producers)}") # Step 4: Let it run await asyncio.sleep(0.3) # Step 5: Modify producer (custom implementation) await channel.modify(some_param="new_value") # Step 6: Remove a consumer await channel.remove_consumer(consumer1) print(f"After removal: {len(channel.get_consumers())} consumers") # Step 7: Stop everything await channel.stop() print("Channel stopped") # Step 8: Flush channel references channel.flush() print(f"Producer channel ref after flush: {prod.channel}") # None # Step 9: Delete channel channels.del_chan("Lifecycle") print("Channel deleted") asyncio.run(main()) ``` -------------------------------- ### Implement SupervisedConsumer with Synchronization in Python Source: https://context7.com/drakkar-software/async-channel/llms.txt Demonstrates SupervisedConsumer usage with task synchronization. Tracks work completion and verifies queue status. Requires async_channel package, asyncio, and uses synchronized channels. ```python import asyncio import async_channel.channels as channels import async_channel.consumer as channel_consumer import async_channel.producer as producer import async_channel.util as util class WorkerProducer(producer.Producer): pass class WorkChannel(channels.Channel): PRODUCER_CLASS = WorkerProducer CONSUMER_CLASS = channel_consumer.SupervisedConsumer async def process_work(task_id, duration): print(f"Starting task {task_id}") await asyncio.sleep(duration) print(f"Completed task {task_id}") async def main(): # Create synchronized channel await util.create_channel_instance( WorkChannel, channels.set_chan, is_synchronized=True ) channel = channels.get_chan("Work") # Add supervised consumer await channel.new_consumer( process_work, priority_level=0 ) # Create producer and send tasks prod = WorkerProducer(channel) await prod.run() await prod.send({"task_id": 1, "duration": 0.1}) await prod.send({"task_id": 2, "duration": 0.1}) await prod.send({"task_id": 3, "duration": 0.1}) print("All tasks queued") # Synchronously process all tasks and wait for completion await prod.synchronized_perform_consumers_queue( priority_level=0, join_consumers=True, timeout=5.0 ) print("All tasks completed") # Verify queue is empty is_empty = prod.is_consumers_queue_empty(priority_level=0) print(f"Queue empty: {is_empty}") # Output: True await channel.stop() asyncio.run(main()) ``` -------------------------------- ### Retrieve consumers with Channel.get_consumer_from_filters() in Python Source: https://context7.com/drakkar-software/async-channel/llms.txt Shows how to query registered consumers based on specific filter dictionaries, including wildcard handling and empty filter to retrieve all consumers. Utilizes async_channel utilities to create a channel instance and demonstrates output of consumer counts for various queries. ```python import asyncio import async_channel.channels as channels import async_channel.util as util import async_channel class FilterChannel(channels.Channel): pass async def handler1(**kwargs): pass async def handler2(**kwargs): pass async def main(): await util.create_channel_instance(FilterChannel, channels.set_chan) channel = channels.get_chan("Filter") # Register consumers with specific filters consumer1 = await channel.new_consumer( handler1, consumer_filters={"market": "BTC", "exchange": "binance"} ) consumer2 = await channel.new_consumer( handler2, consumer_filters={"market": "ETH",exchange": async_channel.CHANNEL_WILDCARD} ) # Find consumers matching filters btc_consumers = channel.get_consumer_from_filters({"market": "BTC"}) print(f"BTC consumers: {len(btc_consumers)}") # Output: BTC consumers: 1 # Wildcard in query matches any exchange eth_consumers = channel.get_consumer_from_filters({ "market": "ETH", "exchange": "kraken" }) print(f"ETH consumers: {len(eth_consumers)}") # Output: ETH consumers: 1 # No match ada_consumers = channel.get_consumer_from_filters({"market": "ADA"}) print(f"ADA consumers: {len(ada_consumers)}") # Output: ADA consumers: 0 # Empty filter returns all consumers all_consumers = channel.get_consumer_from_filters({}) print(f"All consumers: {len(all_consumers)}") # Output: All consumers: 2 await channel.stop() asyncio.run(main()) ``` -------------------------------- ### Complex Filter Matching with Lists in Python Source: https://context7.com/drakkar-software/async-channel/llms.txt Demonstrates how to use list-based filters for consumer routing in async channels. It shows how to set up consumers that match multiple values for specific keys like 'market' and 'type', using wildcards for broader matching. This allows for flexible routing of messages to appropriate handlers based on complex criteria. ```python import asyncio import async_channel.channels as channels import async_channel.util as util import async_channel class MarketChannel(channels.Channel): pass async def crypto_handler(**kwargs): print(f"[CRYPTO] {kwargs}") async def forex_handler(**kwargs): print(f"[FOREX] {kwargs}") async def multi_market_handler(**kwargs): print(f"[MULTI] {kwargs}") async def main(): await util.create_channel_instance(MarketChannel, channels.set_chan) channel = channels.get_chan("Market") # Consumer 1: Accepts multiple crypto markets await channel.new_consumer( crypto_handler, consumer_filters={ "market": ["BTC", "ETH", "ADA"], "type": "crypto" } ) # Consumer 2: Accepts forex only await channel.new_consumer( forex_handler, consumer_filters={ "market": ["EUR", "USD", "GBP"], "type": "forex" } ) # Consumer 3: Accepts multiple types from any market await channel.new_consumer( multi_market_handler, consumer_filters={ "market": async_channel.CHANNEL_WILDCARD, "type": ["crypto", "forex", "stock"] } ) # Send various market data internal_prod = channel.get_internal_producer() await internal_prod.send({"market": "BTC", "type": "crypto", "price": 50000}) # Matches crypto_handler and multi_market_handler await internal_prod.send({"market": "USD", "type": "forex", "rate": 1.2}) # Matches forex_handler and multi_market_handler await internal_prod.send({"market": "AAPL", "type": "stock", "price": 150}) # Matches only multi_market_handler await asyncio.sleep(0.1) # Query consumers by filters btc_consumers = channel.get_consumer_from_filters({"market": "BTC", "type": "crypto"}) print(f"BTC consumers: {len(btc_consumers)}") # Output: 2 await channel.stop() asyncio.run(main()) ``` -------------------------------- ### Implement InternalConsumer with Callbacks in Python Source: https://context7.com/drakkar-software/async-channel/llms.txt Shows how to create InternalConsumer subclasses with custom internal_callback methods. Logs messages and counts events. Requires async_channel package and asyncio. ```python import asyncio import async_channel.channels as channels import async_channel.consumer as channel_consumer import async_channel.util as util class LoggingConsumer(channel_consumer.InternalConsumer): def __init__(self): super().__init__() self.messages = [] async def internal_callback(self, **kwargs): self.messages.append(kwargs) print(f"Logged: {kwargs}") class AnalyticsConsumer(channel_consumer.InternalConsumer): def __init__(self): super().__init__() self.count = 0 async def internal_callback(self, event_type, **kwargs): self.count += 1 print(f"Event #{self.count}: {event_type}") class LogChannel(channels.Channel): CONSUMER_CLASS = channel_consumer.InternalConsumer async def main(): await util.create_channel_instance(LogChannel, channels.set_chan) channel = channels.get_chan("Log") # Add internal consumers logger = LoggingConsumer() await channel.new_consumer(internal_consumer=logger) analytics = AnalyticsConsumer() await channel.new_consumer(internal_consumer=analytics) # Send data internal_prod = channel.get_internal_producer() await internal_prod.send({"event_type": "user_action", "action": "click"}) await internal_prod.send({"event_type": "api_call", "endpoint": "/users"}) await asyncio.sleep(0.1) print(f"Total messages logged: {len(logger.messages)}") # Output: 2 print(f"Total events counted: {analytics.count}") # Output: 2 await channel.stop() asyncio.run(main()) ``` -------------------------------- ### Create filtered consumers with Channel.new_consumer() in Python Source: https://context7.com/drakkar-software/async-channel/llms.txt Demonstrates registering multiple consumers that receive messages based on filter criteria, including usage of a wildcard to handle all events. Requires async_channel library, asyncio, and utilizes internal producer to send test events. Outputs messages handled by appropriate consumer functions. ```python import asyncio import async_channel.channels as channels import async_channel.util as util import async_channel class EventChannel(channels.Channel): pass async def urgent_handler(**kwargs): print(f"[URGENT] {kwargs}") async def trade_handler(**kwargs): print(f"[TRADE] {kwargs}") async def general_handler(**kwargs): print(f"[GENERAL] {kwargs}") async def main(): await util.create_channel_instance(EventChannel, channels.set_chan) channel = channels.get_chan("Event") # Consumer 1: Only receives "urgent" events await channel.new_consumer( urgent_handler, consumer_filters={"event_type": "urgent"} ) # Consumer 2: Only receives "trade" events await channel.new_consumer( trade_handler, consumer_filters={"event_type": "trade"} ) # Consumer 3: Receives ALL events (wildcard) await channel.new_consumer( general_handler, consumer_filters={"event_type": async_channel.CHANNEL_WILDCARD} ) # Send various events through internal producer internal_prod = channel.get_internal_producer() await internal_prod.send({"event_type": "urgent", "data": "Critical error!"}) await internal_prod.send({"event_type": "trade", "data": "BTC/USD bought"}) await internal_prod.send({"event_type": "info", "data": "System status OK"}) await asyncio.sleep(0.1) await channel.stop() asyncio.run(main()) ``` -------------------------------- ### Python: Managing Consumer Priority Levels in Async Channels Source: https://context7.com/drakkar-software/async-channel/llms.txt Demonstrates how to assign and retrieve consumers based on priority levels (HIGH, MEDIUM, OPTIONAL) in Python using the async-channel library. This allows for granular control over resource allocation by prioritizing critical tasks. ```python import asyncio import async_channel.channels as channels import async_channel.consumer as consumer import async_channel.producer as producer import async_channel.util as util import async_channel class PriorityChannel(channels.Channel): pass async def critical_handler(**kwargs): print(f"[HIGH PRIORITY] {kwargs}") async def normal_handler(**kwargs): print(f"[MEDIUM PRIORITY] {kwargs}") async def optional_handler(**kwargs): print(f"[OPTIONAL] {kwargs}") async def main(): await util.create_channel_instance(PriorityChannel, channels.set_chan) channel = channels.get_chan("Priority") # Register consumers with different priority levels await channel.new_consumer( critical_handler, priority_level=async_channel.ChannelConsumerPriorityLevels.HIGH.value # 0 ) await channel.new_consumer( normal_handler, priority_level=async_channel.ChannelConsumerPriorityLevels.MEDIUM.value # 1 ) await channel.new_consumer( optional_handler, priority_level=async_channel.ChannelConsumerPriorityLevels.OPTIONAL.value # 2 ) # Send data internal_prod = channel.get_internal_producer() await internal_prod.send({"msg": "Important update"}) await asyncio.sleep(0.1) # Get consumers by priority high_priority = channel.get_prioritized_consumers( async_channel.ChannelConsumerPriorityLevels.HIGH.value ) print(f"High priority consumers: {len(high_priority)}") # Output: 1 medium_and_above = channel.get_prioritized_consumers( async_channel.ChannelConsumerPriorityLevels.MEDIUM.value ) print(f"Medium+ priority consumers: {len(medium_and_above)}") # Output: 2 await channel.stop() asyncio.run(main()) ``` -------------------------------- ### Wait for Producer Processing in Python Source: https://context7.com/drakkar-software/async-channel/llms.txt This snippet demonstrates how to use `wait_for_processing()` to wait for all supervised consumers to finish processing their queues. It utilizes a `BatchProducer` and `BatchChannel` to enqueue items and subsequently wait for their completion. This involves setting up asyncio, channel and consumer classes and a main function. ```python import asyncio import async_channel.channels as channels import async_channel.consumer as channel_consumer import async_channel.producer as producer import async_channel.util as util class BatchProducer(producer.Producer): async def process_batch(self, items): # Send all items for item in items: await self.send({"item": item}) # Wait for all consumers to finish print("Waiting for consumers to process...") await self.wait_for_processing() print("All consumers finished!") class BatchChannel(channels.Channel): PRODUCER_CLASS = BatchProducer CONSUMER_CLASS = channel_consumer.SupervisedConsumer async def slow_processor(item): await asyncio.sleep(0.1) print(f"Processed: {item}") async def fast_processor(item): print(f"Quick process: {item}") async def main(): await util.create_channel_instance(BatchChannel, channels.set_chan) channel = channels.get_chan("Batch") # Add multiple supervised consumers await channel.new_consumer(slow_processor) await channel.new_consumer(fast_processor) prod = BatchProducer(channel) await prod.run() # Process batch and wait for completion await prod.process_batch(["A", "B", "C"]) await channel.stop() asyncio.run(main()) ``` -------------------------------- ### Python: Producer Pause/Resume Based on Priority Consumers in Async Channels Source: https://context7.com/drakkar-software/async-channel/llms.txt Illustrates how producers in async-channel automatically pause when no high-priority consumers are registered and resume when they become available. This ensures efficient resource usage by only producing when there are critical consumers to process data. ```python import asyncio import async_channel.channels as channels import async_channel.producer as producer import async_channel.util as util import async_channel class MonitoredProducer(producer.Producer): async def start(self): counter = 0 while not self.should_stop: if self.is_running: await self.send({"count": counter}) counter += 1 await asyncio.sleep(0.1) async def pause(self): print("Producer paused - no priority consumers") await super().pause() async def resume(self): print("Producer resumed - priority consumers added") await super().resume() class MonitoredChannel(channels.Channel): PRODUCER_CLASS = MonitoredProducer async def handler(**kwargs): print(f"Received: {kwargs}") async def main(): await util.create_channel_instance(MonitoredChannel, channels.set_chan) channel = channels.get_chan("Monitored") prod = MonitoredProducer(channel) await prod.run() print(f"Initially paused: {channel.is_paused}") # True (no consumers) await asyncio.sleep(0.2) # Add HIGH priority consumer - triggers resume consumer1 = await channel.new_consumer( handler, priority_level=async_channel.ChannelConsumerPriorityLevels.HIGH.value ) print(f"After HIGH consumer: {channel.is_paused}") # False await asyncio.sleep(0.2) # Remove HIGH consumer - triggers pause await channel.remove_consumer(consumer1) print(f"After removing HIGH: {channel.is_paused}") # True # Add OPTIONAL consumer - stays paused await channel.new_consumer( handler, priority_level=async_channel.ChannelConsumerPriorityLevels.OPTIONAL.value ) print(f"After OPTIONAL consumer: {channel.is_paused}") # True await channel.stop() asyncio.run(main()) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.