Basic Service
gRPC service implementation with Hello, Talk, and Background methods
basic_service
πͺ Basic Service Implementation - Where gRPC Magic Happens!
This module contains the main business logic for the BasicService, implementing a delightfully interactive gRPC service that can:
- Greet you like a friendly neighbor π
- Chat with you using an AI therapist (Eliza) π§
- Run background tasks like a multitasking wizard β‘
The service demonstrates various gRPC patterns including unary calls, streaming, and background task orchestration with proper error handling and CloudEvents.
Author: The gRPC Service Squad π¦ΈββοΈπ¦ΈββοΈ
Classes
BasicServiceImpl
Bases: BasicServiceServicer
π The star of the show - BasicService implementation!
This class implements the BasicService gRPC interface, providing three main functionalities wrapped in CloudEvents for that extra enterprise sparkle β¨:
- Hello - A simple greeting service that says hello back
- Talk - A streaming chat service powered by the Eliza chatbot
- Background - Parallel task execution with real-time progress streaming
Each method demonstrates different gRPC patterns:
- Unary RPC (Hello)
- Bidirectional streaming (Talk)
- Server streaming with background tasks (Background)
Source code in services/basic_service.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | |
Functions
Background
async
β‘ The multitasking maestro - run multiple background tasks in parallel!
This method demonstrates advanced async patterns by spinning up multiple background workers that simulate calling various services. It provides real-time progress updates via server streaming and handles errors gracefully.
Perfect for demonstrating:
- Parallel task execution with asyncio
- Server-side streaming with progress updates
- Error handling and recovery in distributed systems
- Task cancellation on client disconnect
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
BackgroundRequest
|
Configuration for background tasks |
required |
context
|
ServicerContext
|
gRPC context for streaming responses |
required |
Yields:
| Type | Description |
|---|---|
|
service_pb2.BackgroundResponse: Stream of progress updates wrapped in CloudEvents |
Request Parameters
- processes (int): Number of parallel workers to spawn (defaults to 1)
Streaming Behavior
- Initial STATE_PROCESS response with empty results
- Updated STATE_PROCESS responses after each task completion
- Final STATE_COMPLETE response with all results
Error Handling
- Individual task failures are captured and included as error responses
- Client cancellation properly stops all background workers
- Transport errors cancel all tasks and propagate the error
Example
Request: BackgroundRequest(processes=3) Stream: [Initial empty, Progress 1/3, Progress 2/3, Progress 3/3, Final complete]
Note
Workers simulate calling random services (REST, gRPC, etc.) with realistic delays. It's like having a team of very dedicated, if imaginary, service callers! πββοΈπββοΈ
Source code in services/basic_service.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | |
Hello
async
π Say hello in the most elaborate way possible!
Takes a simple message and wraps it in a CloudEvent because even greetings deserve enterprise-grade packaging. This demonstrates the basic unary RPC pattern with CloudEvents integration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
HelloRequest
|
The incoming hello request with a message |
required |
context
|
ServicerContext
|
gRPC service context (standard gRPC magic) |
required |
Returns:
| Type | Description |
|---|---|
HelloResponse
|
service_pb2.HelloResponse: A CloudEvent-wrapped greeting response |
Example
Input: HelloRequest(message="World") Output: HelloResponse containing CloudEvent with "Hello, World"
Note
Each response gets a unique UUID and timestamp because we're fancy like that! π
Source code in services/basic_service.py
Talk
async
π§ Have a therapeutic chat with our resident AI psychologist!
This streaming method connects you with Eliza, the classic chatbot therapist. Send messages and receive thoughtful (or seemingly thoughtful) responses in real-time. It's like having a conversation with a very patient, if somewhat repetitive, therapist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request_iterator
|
Async iterator of TalkRequest messages from the client |
required | |
context
|
ServicerContext
|
gRPC service context for streaming magic |
required |
Yields:
| Type | Description |
|---|---|
|
service_pb2.TalkResponse: Streaming responses from our AI therapist |
Features
- Bidirectional streaming (talk and listen simultaneously)
- Built-in Eliza chatbot for therapeutic conversations
- Debug logging for conversation tracking
- Handles client disconnection gracefully
Example Conversation
Client: "I feel sad today" Eliza: "I am sorry to hear that you are sad." Client: "Why do I feel this way?" Eliza: "Why do you say that?"
Note
Eliza might seem repetitive, but that's part of her charm! She's been doing this since 1966, so she's got experience. π΅