Getting Started with mtaio¶
This guide will help you get started with mtaio framework. We'll cover installation, basic concepts, and create a simple application.
Installation¶
First, install mtaio using pip:
Basic Concepts¶
mtaio is built around several core concepts:
- Async-First: Everything is designed to work with Python's
asyncio
- Resource Management: Built-in tools for handling system resources efficiently
- Event-Driven: Event-based architecture for building reactive applications
- Type Safety: Full type hints support for better development experience
Your First mtaio Application¶
Let's create a simple application that demonstrates the basic features of mtaio.
import asyncio
from mtaio.events import EventEmitter
from mtaio.cache import TTLCache
from mtaio.core import TaskExecutor
# Create a data processor class
class DataProcessor:
def __init__(self):
self.emitter = EventEmitter()
self.cache = TTLCache[str](default_ttl=60.0) # 60 seconds TTL
self.executor = TaskExecutor()
async def process_data(self, data: str) -> str:
# Check cache first
cached_result = await self.cache.get(data)
if cached_result is not None:
await self.emitter.emit("cache_hit", data)
return cached_result
# Process data
async with self.executor as executor:
result = await executor.run(self.compute_result, data)
# Cache the result
await self.cache.set(data, result)
await self.emitter.emit("process_complete", result)
return result
async def compute_result(self, data: str) -> str:
# Simulate some heavy computation
await asyncio.sleep(1)
return data.upper()
# Create event handlers
async def handle_cache_hit(event):
print(f"Cache hit for data: {event.data}")
async def handle_process_complete(event):
print(f"Processing completed with result: {event.data}")
# Main application
async def main():
# Initialize processor
processor = DataProcessor()
# Register event handlers
processor.emitter.on("cache_hit")(handle_cache_hit)
processor.emitter.on("process_complete")(handle_process_complete)
# Process some data
data_items = ["hello", "world", "hello", "mtaio"]
for data in data_items:
result = await processor.process_data(data)
print(f"Result for '{data}': {result}")
# Run the application
if __name__ == "__main__":
asyncio.run(main())
This example demonstrates:
- Event handling using
EventEmitter
- Caching with
TTLCache
- Task execution with
TaskExecutor
- Proper async/await usage
Next Steps¶
Once you're comfortable with the basics, you can:
- Learn more about mtaio's core features
- Explore advanced usage patterns
- Check out the API reference
- See more examples in our repository
Common Patterns¶
Here are some common patterns you'll use in mtaio applications:
Resource Management¶
from mtaio.resources import RateLimiter
limiter = RateLimiter(10.0) # 10 operations per second
@limiter.limit
async def rate_limited_operation():
# Your code here
pass
Event-Driven Architecture¶
from mtaio.events import EventEmitter
emitter = EventEmitter()
@emitter.on("event_name")
async def handle_event(event):
# Handle event
pass
# Emit events
await emitter.emit("event_name", data)
Caching Strategies¶
from mtaio.cache import TTLCache
from mtaio.decorators import with_cache
cache = TTLCache[str](default_ttl=300.0) # 5 minutes
@with_cache(cache)
async def cached_operation(key: str) -> str:
# Expensive operation
return result
Best Practices¶
-
Type Hints: Always use type hints for better code quality and IDE support:
-
Resource Cleanup: Use async context managers for proper resource cleanup:
-
Error Handling: Use mtaio's exception hierarchy for proper error handling:
-
Configuration: Keep configuration separate and use environment variables:
Getting Help¶
If you encounter any issues:
- Check the Troubleshooting guide
- Search for similar issues in our GitHub repository
- Create a new issue if your problem hasn't been addressed
Next, proceed to Basic Usage for more detailed information about mtaio's features.