StreamEngine
kstreams.engine.StreamEngine
Attributes:
Name | Type | Description |
---|---|---|
backend |
Kafka
|
Backend to connect. Default |
consumer_class |
Consumer
|
The consumer class to use when instanciate a consumer. Default kstreams.Consumer |
producer_class |
Producer
|
The producer class to use when instanciate the producer. Default kstreams.Producer |
monitor |
PrometheusMonitor
|
Prometheus monitor that holds the metrics |
title |
str | None
|
Engine name |
serializer |
Serializer | None
|
Serializer to use when an event is produced. |
deserializer |
Deserializer | None
|
Deserializer to be used when an event is consumed. If provided it will be used in all Streams instances as a general one. To override it per Stream, you can provide one per Stream |
Example
import kstreams
stream_engine = kstreams.create_engine(
title="my-stream-engine"
)
@kstreams.stream("local--hello-world", group_id="example-group")
async def consume(stream: kstreams.ConsumerRecord) -> None:
print(f"showing bytes: {cr.value}")
await stream_engine.start()
Source code in kstreams/engine.py
27 28 29 30 31 32 33 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 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
|
send(topic, value=None, key=None, partition=None, timestamp_ms=None, headers=None, serializer=None, serializer_kwargs=None)
async
Attributes:
Name | Type | Description |
---|---|---|
topic |
str
|
Topic name to send the event to |
value |
Any
|
Event value |
key |
str | None
|
Event key |
partition |
int | None
|
Topic partition |
timestamp_ms |
int | None
|
Event timestamp in miliseconds |
headers |
Dict[str, str] | None
|
Event headers |
serializer |
Serializer | None
|
Serializer to encode the event |
serializer_kwargs |
Dict[str, Any] | None
|
Serializer kwargs |
Source code in kstreams/engine.py
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 |
|
on_startup(func)
A list of callables to run before the engine starts. Handler are callables that do not take any arguments, and may be either standard functions, or async functions.
Attributes:
Name | Type | Description |
---|---|---|
func |
Callable[[], Any]
|
Func to callable before engine starts |
Example
import kstreams
stream_engine = kstreams.create_engine(
title="my-stream-engine"
)
@stream_engine.on_startup
async def init_db() -> None:
print("Initializing Database Connections")
await init_db()
@stream_engine.on_startup
async def start_background_task() -> None:
print("Some background task")
Source code in kstreams/engine.py
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 |
|
on_stop(func)
A list of callables to run before the engine stops. Handler are callables that do not take any arguments, and may be either standard functions, or async functions.
Attributes:
Name | Type | Description |
---|---|---|
func |
Callable[[], Any]
|
Func to callable before engine stops |
Example
import kstreams
stream_engine = kstreams.create_engine(
title="my-stream-engine"
)
@stream_engine.on_stop
async def close_db() -> None:
print("Closing Database Connections")
await db_close()
Source code in kstreams/engine.py
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 |
|
after_startup(func)
A list of callables to run after the engine starts. Handler are callables that do not take any arguments, and may be either standard functions, or async functions.
Attributes:
Name | Type | Description |
---|---|---|
func |
Callable[[], Any]
|
Func to callable after engine starts |
Example
import kstreams
stream_engine = kstreams.create_engine(
title="my-stream-engine"
)
@stream_engine.after_startup
async def after_startup() -> None:
print("Set pod as healthy")
await mark_healthy_pod()
Source code in kstreams/engine.py
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 |
|
after_stop(func)
A list of callables to run after the engine stops. Handler are callables that do not take any arguments, and may be either standard functions, or async functions.
Attributes:
Name | Type | Description |
---|---|---|
func |
Callable[[], Any]
|
Func to callable after engine stops |
Example
import kstreams
stream_engine = kstreams.create_engine(
title="my-stream-engine"
)
@stream_engine.after_stop
async def after_stop() -> None:
print("Finishing backgrpund tasks")
Source code in kstreams/engine.py
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 |
|
add_stream(stream, error_policy=None)
Add a stream to the engine.
This method registers a new stream with the engine, setting up necessary configurations and handlers. If a stream with the same name already exists, a DuplicateStreamException is raised.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stream
|
Stream
|
The stream to be added. |
required |
error_policy
|
Optional[StreamErrorPolicy]
|
An optional error policy to be applied to the stream. You should probably set directly when instanciating a Stream, not here. |
None
|
Raises:
Type | Description |
---|---|
DuplicateStreamException
|
If a stream with the same name already exists. |
Notes
- If the stream does not have a deserializer, the engine's deserializer is assigned to it.
- If the stream does not have a rebalance listener, a default MetricsRebalanceListener is assigned.
- The stream's UDF handler is set up with the provided function and engine's send method.
- If the stream's UDF handler type is not NO_TYPING, a middleware stack is built for the stream's function.
Source code in kstreams/engine.py
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
|