Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 66 additions & 9 deletions workspace/7_framework/sanic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ sanic/
│ │ └── error_handlers.py
│ ├── main.py
│ └── README.md
├── day3_sqlite/ # Advanced Sanic with database integration
│ ├── app/
│ │ ├── __init__.py
│ │ ├── routes.py
│ │ ├── database.py
│ │ ├── error_handlers.py
│ │ └── config.py
│ ├── main.py
│ ├── requirements.txt
│ └── README.md
└── README.md # This file
```

Expand Down Expand Up @@ -120,15 +130,21 @@ async def handler(request):

Sanic is designed for speed. Here's how it compares to other Python frameworks:

| Framework | Requests/sec | Relative Speed |
|-----------|-------------|----------------|
| Sanic | ~40,000 | 1.0x |
| FastAPI | ~30,000 | 0.75x |
| Flask | ~5,000 | 0.125x |
| Django | ~3,000 | 0.075x |
| Framework | Requests/sec | Relative Speed | Use Case |
|-----------|-------------|----------------|----------|
| Sanic | ~40,000 | 1.0x | High-performance APIs, real-time apps |
| FastAPI | ~30,000 | 0.75x | Modern APIs with auto-documentation |
| Flask | ~5,000 | 0.125x | Simple web apps, prototyping |
| Django | ~3,000 | 0.075x | Full-featured web applications |

*Note: Performance varies based on application complexity and server configuration*

### Day 3 Performance Features
- **Database Connection Pooling**: Efficient async SQLite operations
- **HTTP Client Optimization**: Connection reuse and timeout management
- **Memory Management**: Proper resource cleanup and cursor handling
- **Configuration Caching**: Environment-based settings loaded once

## When to Use Sanic

### Good For:
Expand All @@ -150,8 +166,11 @@ Sanic is designed for speed. Here's how it compares to other Python frameworks:
3. **Practice Routing** - Try different route patterns
4. **Learn Intermediate Concepts** - Explore the `day2_intermediate/` directory
5. **Add Middleware** - Implement request/response processing
6. **Database Integration** - Connect to databases with async drivers
7. **Production Deployment** - Learn deployment strategies
6. **Master Advanced Features** - Explore the `day3_sqlite/` directory
7. **Database Integration** - Learn async SQLite operations and data persistence
8. **External Service Integration** - Make HTTP requests to external APIs
9. **Configuration Management** - Implement environment-based configuration
10. **Production Deployment** - Learn deployment strategies and monitoring

## Resources

Expand Down Expand Up @@ -185,6 +204,16 @@ The examples in this directory use Sanic 22.3.0, which provides:
- Application lifecycle listeners
- Multi-worker deployment

### Day 3 - Advanced Features (`day3_sqlite/`)
- **Query Parameters**: Handle URL query parameters with validation
- **Typed Route Parameters**: Automatic type conversion and validation
- **JSON Validation**: Robust request body validation with detailed error messages
- **Async HTTP Client**: External API calls with httpx and comprehensive error handling
- **Async SQLite Integration**: Complete database operations with aiosqlite
- **Configuration Management**: Environment-based configuration with dataclasses
- **Enhanced Logging**: Structured logging throughout the application
- **Production Patterns**: Error handling, resource management, and security best practices

## Getting Started

1. **Begin with Day 1**: Navigate to the `day1_basic/` directory
Expand All @@ -193,6 +222,34 @@ The examples in this directory use Sanic 22.3.0, which provides:
4. **Progress to Day 2**: Navigate to the `day2_intermediate/` directory
5. Follow the setup instructions in `day2_intermediate/README.md`
6. Explore middleware, error handling, and blueprints
7. Experiment with the code to understand how it works
7. **Advance to Day 3**: Navigate to the `day3_sqlite/` directory
8. Follow the setup instructions in `day3_sqlite/README.md`
9. Learn database integration, external APIs, and advanced validation
10. Practice with query parameters, typed routes, and JSON validation
11. Experiment with the configuration system and logging
12. Test all endpoints and error handling scenarios

## Tutorial Progression Comparison

| Feature | Day 1 | Day 2 | Day 3 |
|---------|-------|-------|-------|
| **Routing** | Basic GET/POST | Blueprints | Query params, typed routes |
| **Responses** | Text, JSON | Custom headers | Enhanced validation |
| **Error Handling** | Basic exceptions | Centralized handlers | Comprehensive logging |
| **Architecture** | Simple structure | Middleware, listeners | Configuration management |
| **Data Storage** | In-memory | None | Async SQLite database |
| **External Services** | None | None | HTTP client with httpx |
| **Configuration** | Hardcoded | Basic setup | Environment-based |
| **Logging** | Print statements | Basic logging | Structured logging |
| **Production Ready** | Development only | Multi-worker | Full production patterns |
| **Complexity** | Beginner | Intermediate | Advanced |

### Learning Path Summary

- **Day 1**: Master the fundamentals of Sanic routing, responses, and async concepts
- **Day 2**: Learn application organization with middleware, blueprints, and error handling
- **Day 3**: Build production-ready applications with databases, external APIs, and proper configuration

Each tutorial builds upon the previous one, creating a comprehensive learning experience that takes you from basic concepts to advanced production patterns.

Happy coding with Sanic!
Loading