Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      The Power Of The Intl API: A Definitive Guide To Browser-Native Internationalization

      August 8, 2025

      This week in AI dev tools: GPT-5, Claude Opus 4.1, and more (August 8, 2025)

      August 8, 2025

      Elastic simplifies log analytics for SREs and developers with launch of Log Essentials

      August 7, 2025

      OpenAI launches GPT-5

      August 7, 2025

      I compared the best headphones from Apple, Sony, Bose, and Sonos: Here’s how the AirPods Max wins

      August 10, 2025

      I changed these 6 settings on my iPad to significantly improve its battery life

      August 10, 2025

      DistroWatch Weekly, Issue 1134

      August 10, 2025

      3 portable power stations I travel everywhere with (and how they differ)

      August 9, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Next.js PWA offline capability with Service Worker, no extra package

      August 10, 2025
      Recent

      Next.js PWA offline capability with Service Worker, no extra package

      August 10, 2025

      spatie/laravel-flare

      August 9, 2025

      Establishing Consistent Data Foundations with Laravel’s Database Population System

      August 8, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Windows 11 Copilot gets free access to GPT-5 Thinking, reduced rate limits than ChatGPT Free

      August 10, 2025
      Recent

      Windows 11 Copilot gets free access to GPT-5 Thinking, reduced rate limits than ChatGPT Free

      August 10, 2025

      Best Architecture AI Rendering Platform: 6 Tools Tested

      August 10, 2025

      Microsoft won’t kill off Chromium Edge and PWAs on Windows 10 until October 2028

      August 10, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Machine Learning»A Code Implementation of a Real‑Time In‑Memory Sensor Alert Pipeline in Google Colab with FastStream, RabbitMQ, TestRabbitBroker, Pydantic

    A Code Implementation of a Real‑Time In‑Memory Sensor Alert Pipeline in Google Colab with FastStream, RabbitMQ, TestRabbitBroker, Pydantic

    April 21, 2025

    In this notebook, we demonstrate how to build a fully in-memory “sensor alert” pipeline in Google Colab using FastStream, a high-performance, Python-native stream processing framework, and its integration with RabbitMQ. By leveraging faststream.rabbit’s RabbitBroker and TestRabbitBroker, we simulate a message broker without needing external infrastructure. We orchestrate four distinct stages: ingestion & validation, normalization, monitoring & alert generation, and archiving, each defined as Pydantic models (RawSensorData, NormalizedData, AlertData) to ensure data quality and type safety. Under the hood, Python’s asyncio powers asynchronous message flow, while nest_asyncio enables nested event loops in Colab. We also employ the standard logging module for traceable pipeline execution and pandas for final result inspection, making it easy to visualize archived alerts in a DataFrame.

    Copy CodeCopiedUse a different Browser
    !pip install -q faststream[rabbit] nest_asyncio

    We install FastStream with its RabbitMQ integration, providing the core stream-processing framework and broker connectors, as well as the nest_asyncio package, which enables nested asyncio event loops in environments like Colab. All this is achieved while keeping the output minimal with the -q flag.

    Copy CodeCopiedUse a different Browser
    import nest_asyncio, asyncio, logging
    nest_asyncio.apply()
    

    We import the nest_asyncio, asyncio, and logging modules, then apply nest_asyncio.apply() to patch Python’s event loop so that you can run nested asynchronous tasks inside environments like Colab or Jupyter notebooks without errors. The logging import readies you to instrument your pipeline with detailed runtime logs.

    Copy CodeCopiedUse a different Browser
    logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
    logger = logging.getLogger("sensor_pipeline")

    We configure Python’s built‑in logging to emit INFO‑level (and above) messages prefixed with a timestamp and severity, then create a dedicated logger named “sensor_pipeline” for emitting structured logs within your streaming pipeline.

    Copy CodeCopiedUse a different Browser
    from faststream import FastStream
    from faststream.rabbit import RabbitBroker, TestRabbitBroker
    from pydantic import BaseModel, Field, validator
    import pandas as pd
    from typing import List

    We bring in FastStream’s core FastStream class alongside its RabbitMQ connectors (RabbitBroker for real brokers and TestRabbitBroker for in‑memory testing), Pydantic’s BaseModel, Field, and validator for declarative data validation, pandas for tabular result inspection, and Python’s List type for annotating our in‑memory archives.

    Copy CodeCopiedUse a different Browser
    broker = RabbitBroker("amqp://guest:guest@localhost:5672/")
    app    = FastStream(broker)
    

    We instantiate a RabbitBroker pointed at a (local) RabbitMQ server using the AMQP URL, then create a FastStream application bound to that broker, setting up the messaging backbone for your pipeline stages.

    Copy CodeCopiedUse a different Browser
    class RawSensorData(BaseModel):
        sensor_id: str       = Field(..., examples=["sensor_1"])
        reading_celsius: float = Field(..., ge=-50, le=150, examples=[23.5])
       
        @validator("sensor_id")
        def must_start_with_sensor(cls, v):
            if not v.startswith("sensor_"):
                raise ValueError("sensor_id must start with 'sensor_'")
            return v
    
    
    class NormalizedData(BaseModel):
        sensor_id: str
        reading_kelvin: float
    
    
    class AlertData(BaseModel):
        sensor_id: str
        reading_kelvin: float
        alert: bool

    These Pydantic models define the schema for each stage: RawSensorData enforces input validity (e.g., reading range and a sensor_ prefix), NormalizedData converts Celsius to Kelvin, and AlertData encapsulates the final alert payload (including a boolean flag), ensuring a type-safe data flow throughout the pipeline.

    Copy CodeCopiedUse a different Browser
    archive: List[AlertData] = []
    
    
    @broker.subscriber("sensor_input")
    @broker.publisher("normalized_input")
    async def ingest_and_validate(raw: RawSensorData) -> dict:
        logger.info(f"Ingested raw data: {raw.json()}")
        return raw.dict()
    
    
    @broker.subscriber("normalized_input")
    @broker.publisher("sensor_alert")
    async def normalize(data: dict) -> dict:
        norm = NormalizedData(
            sensor_id=data["sensor_id"],
            reading_kelvin=data["reading_celsius"] + 273.15
        )
        logger.info(f"Normalized to Kelvin: {norm.json()}")
        return norm.dict()
    
    
    ALERT_THRESHOLD_K = 323.15  
       
    @broker.subscriber("sensor_alert")
    @broker.publisher("archive_topic")
    async def monitor(data: dict) -> dict:
        alert_flag = data["reading_kelvin"] > ALERT_THRESHOLD_K
        alert = AlertData(
            sensor_id=data["sensor_id"],
            reading_kelvin=data["reading_kelvin"],
            alert=alert_flag
        )
        logger.info(f"Monitor result: {alert.json()}")
        return alert.dict()
    
    
    @broker.subscriber("archive_topic")
    async def archive_data(payload: dict):
        rec = AlertData(**payload)
        archive.append(rec)
        logger.info(f"Archived: {rec.json()}")

    An in-memory archive list collects all finalized alerts, while four asynchronous functions, wired via @broker.subscriber/@broker.publisher, form the pipeline stages. These functions ingest and validate raw sensor inputs, convert Celsius to Kelvin, check against an alert threshold, and finally archive each AlertData record, emitting logs at every step for full traceability.

    Copy CodeCopiedUse a different Browser
    async def main():
        readings = [
            {"sensor_id": "sensor_1", "reading_celsius": 45.2},
            {"sensor_id": "sensor_2", "reading_celsius": 75.1},
            {"sensor_id": "sensor_3", "reading_celsius": 50.0},
        ]
        async with TestRabbitBroker(broker) as tb:
            for r in readings:
                await tb.publish(r, "sensor_input")
            await asyncio.sleep(0.1)
           
        df = pd.DataFrame([a.dict() for a in archive])
        print("nFinal Archived Alerts:")
        display(df)
    
    
    asyncio.run(main())
    

    Finally, the main coroutine publishes a set of sample sensor readings into the in-memory TestRabbitBroker, pauses briefly to allow each pipeline stage to run, and then collates the resulting AlertData records from the archive into a pandas DataFrame for easy display and verification of the end-to-end alert flow. At the end, asyncio.run(main()) kicks off the entire async demo in Colab.

    In conclusion, this tutorial demonstrates how FastStream, combined with RabbitMQ abstractions and in-memory testing via TestRabbitBroker, can accelerate the development of real-time data pipelines without the overhead of deploying external brokers. With Pydantic handling schema validation, asyncio managing concurrency, and pandas enabling quick data analysis, this pattern provides a robust foundation for sensor monitoring, ETL tasks, or event‑driven workflows. You can seamlessly transition from this in‑memory demo to production by swapping in a live broker URL (RabbitMQ, Kafka, NATS, or Redis) and running faststream run under uvicorn or your preferred ASGI server, unlocking scalable, maintainable stream processing in any Python environment.


    Here is the Colab Notebook. Also, don’t forget to follow us on Twitter and join our Telegram Channel and LinkedIn Group. Don’t Forget to join our 90k+ ML SubReddit.

    🔥 [Register Now] miniCON Virtual Conference on AGENTIC AI: FREE REGISTRATION + Certificate of Attendance + 4 Hour Short Event (May 21, 9 am- 1 pm PST) + Hands on Workshop

    The post A Code Implementation of a Real‑Time In‑Memory Sensor Alert Pipeline in Google Colab with FastStream, RabbitMQ, TestRabbitBroker, Pydantic appeared first on MarkTechPost.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleBuild an automated generative AI solution evaluation pipeline with Amazon Nova
    Next Article Anthropic Releases a Comprehensive Guide to Building Coding Agents with Claude Code

    Related Posts

    Machine Learning

    How to Evaluate Jailbreak Methods: A Case Study with the StrongREJECT Benchmark

    August 10, 2025
    Machine Learning

    AI Agent Trends of 2025: A Transformative Landscape

    August 10, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    How Universal Design and GAAD Are Connected

    Development

    CVE-2025-4128 – Mattermost Guest User API Team Information Disclosure

    Common Vulnerabilities and Exposures (CVEs)

    Amazon’s AI Boom Collides with Climate Pledge: Carbon Emissions Soar 6%

    Security

    CVE-2025-48207 – TYPO3 Reint Download Manager Insecure Direct Object Reference (IDOR)

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    CVE-2025-5755 – SourceCodester Open Source Clinic Management System SQL Injection

    June 6, 2025

    CVE ID : CVE-2025-5755

    Published : June 6, 2025, 10:15 a.m. | 1 hour, 34 minutes ago

    Description : A vulnerability was found in SourceCodester Open Source Clinic Management System 1.0. It has been classified as critical. Affected is an unknown function of the file /email_config.php. The manipulation of the argument email leads to sql injection. It is possible to launch the attack remotely. The exploit has been disclosed to the public and may be used.

    Severity: 7.3 | HIGH

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    CVE-2025-6290 – WordPress Tournament Bracket Generator Stored Cross-Site Scripting

    June 26, 2025

    How to Become an Analytical Programmer – Solve the “Rock, Paper, Scissors” Game 5 Ways Using JavaScript & Mermaid.js

    May 13, 2025

    14 Best SEO Tools for Agencies to Boost Client Results in 2025

    July 16, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.