{"id":16490523,"url":"https://github.com/dave-wind/awesome-python","last_synced_at":"2026-05-13T00:35:36.700Z","repository":{"id":247108899,"uuid":"825042854","full_name":"dave-wind/awesome-python","owner":"dave-wind","description":"records awesome py IOT project","archived":false,"fork":false,"pushed_at":"2024-07-07T12:47:03.000Z","size":21,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-16T11:09:49.661Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dave-wind.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-06T15:51:48.000Z","updated_at":"2024-07-07T12:47:06.000Z","dependencies_parsed_at":"2024-10-19T03:28:26.170Z","dependency_job_id":null,"html_url":"https://github.com/dave-wind/awesome-python","commit_stats":null,"previous_names":["dave-wind/awesome-python"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/dave-wind/awesome-python","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dave-wind%2Fawesome-python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dave-wind%2Fawesome-python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dave-wind%2Fawesome-python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dave-wind%2Fawesome-python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dave-wind","download_url":"https://codeload.github.com/dave-wind/awesome-python/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dave-wind%2Fawesome-python/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32963172,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-12T23:30:32.555Z","status":"ssl_error","status_checked_at":"2026-05-12T23:30:18.191Z","response_time":102,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T13:48:16.926Z","updated_at":"2026-05-13T00:35:36.676Z","avatar_url":"https://github.com/dave-wind.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# awesome-python\nrecords awesome py IOT project\n\n### aiomqtt + fastapi 【app.py】\n\n#### 是什么？  \n本例子 主要是适用于工业IOT场景，需要消费大量实时工艺数据,使用深度模型预测性监控工艺的合格度，从而提高产品品控，减少人力投入，提高ROI指标  \n#### 为什么？  \n为什么使用python？因为很多深度模型需要python；需要一个后台服务，所以写了基于fastapi服务 + aiomqtt 异步消费mqtt\n#### 怎么做?  \n1.使用asyncio 异步协程 处理高并发问题  \n@contextlib.asynccontextmanager 允许你通过定义异步上下文管理器，在不同生命周期阶段执行特定的异步操作。在 FastAPI 的 lifespan 中使用这种方式，可以确保在应用程序启动和关闭时，能够按照预期地管理异步任务和资源\n在异步函数中，yield 通常结合 asyncio 的相关方法使用，用于暂停当前函数的执行，并将控制权返回给事件循环（event loop）\nyield之后 类似于 before unmounted Server【服务卸载之前】执行的操作\n\n```python3\n@contextlib.asynccontextmanager\nasync def lifespan(app: FastAPI):\n    try:\n        task = asyncio.create_task(run_mqtt())\n        yield\n        task.cancel()\n    except Exception as e:\n            print(f\"Task cancelled {e}\")\n            \n\napp = FastAPI(lifespan=lifespan)\n\n```\n\n\n2.mqtt 使用 aiomqtt 异步订阅topic，mqtt 订阅topic 和 发送端 最好使用 Qos 为 1；提高消息接收率,mqtt Qos如下  \n```txt\nQoS 0 - 最多一次传递（At most once）：\n这是最低的服务质量级别，消息发布者发送消息后，不会收到任何确认。消息可能会丢失或重复，也没有重新传输的机制。适用于实时性要求不高，且消息丢失或重复对系统影响不大的场景。\nQoS 1 - 至少一次传递（At least once）：\n在这个级别下，消息发布者会收到一个确认（PUBACK）消息，确保消息至少被传递一次给订阅者。如果发布者没有收到确认，它会重新发送消息，这样可以确保消息最终被接收。这种确认机制保证了消息不会丢失，但可能会导致消息重复\nQoS 2 - 刚好一次传递（Exactly once）：\n这是最高的服务质量级别，确保消息被精确地传递一次。在 QoS 2 中，除了发布者和订阅者之间的确认（PUBREC、PUBREL 和 PUBCOMP）外，还使用了消息排重和顺序控制。虽然 QoS 2 提供了最高的可靠性，但由于需要更多的通信开销和处理，因此它可能会导致一些性能损失。\n```\n\n3.使用async with 异步编程中处理上下文管理器，自动处理资源释放，降低了资源泄漏的风险\n\n```python3\n   while True:\n        try:\n            async with aiomqtt.Client('xxxx',1883,username='xxx',password='xxxxx') as c:\n                client = c\n                await asyncio.gather(*[subscribe_to_topic(client, topic) for topic in topics])\n\n                loop = asyncio.get_event_loop()\n                async for message in c.messages:\n                     loop.create_task(foo(message))\n        except aiomqtt.MqttError as e:\n            print(f\"Connection lost due to {e}; Reconnecting in {interval} seconds ...\")\n            await asyncio.sleep(interval)\n\n```\n\n4.关于并发问题方面，使用 asyncio.Semaphore(10) + asyn with 【异步上下文】 来解决并发问题，如果1秒钟有20条数据，我们可以控制并发数 使用 asyncio 强大的协程并发能力 处理大量消费数据 效率极高\n```python3\nasync def subscribe_to_topic(client, topic):\n        async with sem:\n            await client.subscribe(topic)\n            await asyncio.sleep(0.1)\n            print(f\"Subscribed to topic: {topic}\")\n        \n\nasync def message_consumer(client):\n    async for message in client.messages:\n        asyncio.create_task(foo(sem,message))\n\n```\n\n5.深度学习 【代码暂时不展示】\n主要使用 数据增强 做数据集，多分类的思想 训练模型，然后构建网络骨架 在fastapi 服务内消费 和判断 工艺曲线数据的合格率\n\n\n6.fastapi 这里主要做 实时数据的接口 利用发布订阅 + Server-Sent Events 技术，单向主推 替代的沉重的双向长连接websocket  以及飞书api 机器人报警服务的推送等功能 和 数据归档落库逻辑\n\n\n【未完待续...】\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdave-wind%2Fawesome-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdave-wind%2Fawesome-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdave-wind%2Fawesome-python/lists"}