{"id":19659790,"url":"https://github.com/maicius/hiredistest","last_synced_at":"2026-07-14T15:46:15.609Z","repository":{"id":82733584,"uuid":"135020681","full_name":"Maicius/HiredisTest","owner":"Maicius","description":"测试C++ Hiredis库","archived":false,"fork":false,"pushed_at":"2018-05-27T06:57:01.000Z","size":55,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-10T01:27:27.221Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","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/Maicius.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":"2018-05-27T06:14:09.000Z","updated_at":"2021-09-24T12:17:49.000Z","dependencies_parsed_at":null,"dependency_job_id":"2e2406da-da62-4103-9d2c-cf27432a1017","html_url":"https://github.com/Maicius/HiredisTest","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Maicius%2FHiredisTest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Maicius%2FHiredisTest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Maicius%2FHiredisTest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Maicius%2FHiredisTest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Maicius","download_url":"https://codeload.github.com/Maicius/HiredisTest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240968812,"owners_count":19886378,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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-11-11T15:44:22.545Z","updated_at":"2025-10-24T20:36:48.874Z","avatar_url":"https://github.com/Maicius.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 基于Redis的进程间通信——在C++里使用python的深度学习模型\n\n- 本文主要是为进程间通信提供一种新的思路\n\n- 博客地址:[基于Redis的进程间通信——在C++里使用python的深度学习模型](http://www.xiaomaidong.com/?p=707)\n\n- 本想法来源于[RoboMaster](https://www.robomaster.com/zh-CN)比赛中的神符检测，神符是指一个9宫格的手写体数字(Mnist)或火焰体动态数字，需要用到机器学习或深度学习模型对数字进行识别，从检测大符到识别全部数字到发射子弹，整个过程不能超过1.5秒，全部的运算量都集中在搭载在机器小车上的miniPC，由于miniPC性能有限，且除了这个程序之外还有个非常占用资源的自动射击程序，所以整个程序使用的是C++的代码。一开始我们使用的是xgboost，在python上把模型训练完成之后，把预测代码再改写成C++。而后来发现xgboost在真实场景上的准确率较低(只有70%)，于是改为了复杂的深度学习模型，准确率上几乎可以达到100%。但此时的python代码已经比较复杂，再全部改成C++已经不现实，所以开始寻找一种能实现两者间通信的方案。\n\n### 网上已有的通信方案\n\n- C++ 与python的混合编程方案比较多，比如使用\u003cpython.h\u003e，说实话，我没认真看，需要的自己去百度或谷歌吧\n\n### 基于Redis的通信方案\n\n#### 基本思路\n\n- Redis是一种基于内存的NoSQL 数据库，所以读写速度非常快，且使用非常简单。\n- 在本应用场景中，每次需要通信的数据是9张图片，每张图片是28x28像素，所以其实就是 9 x 28 x 28的矩阵\n- 所以，在C++程序中进行检测和提取图像，将9张图片的像素值依次读取进来，存储进Redis，通过标志位的方式告诉python程序进行读取，完成预测之后再将结果存储进redis，并让C++程序进行调用。整个程序的活动图如下：\n- 得益于redis的高效读取，在没有gpu的加速下，在mac i7 16G的配置上跑完一次读取和预测大概需要0.3秒，即使在性能较差的miniPC上，一次耗时大概在0.6秒，主要时间消耗都在预测上，连接和读取的时间几乎可以忽略不计\n\n\n#### 代码实现\n\n- C++ 使用hiredis\n\n\t\tmac上安装方法：brew install hiredis\n\t\tubuntu安装方法：apt-get install hiredis\n\n- demo1: 连接数据库\n\n\t\tredisContext* connect_redis(){\n    \t\tredisContext *context = redisConnect(\"127.0.0.1\", 6379);\n    \t\tif(context-\u003eerr) {\n        \t\tredisFree(context);\n        \t\tprintf(\"connect redisServer err:%s\\n\", context-\u003eerrstr);\n        \t\treturn NULL;\n    \t\t}\n    \t\tprintf(\"connect redisServer success\\n\");\n    \t\treturn context;\n\t\t}\n\n- demo2: 测试redis lrange 操作\n\n\t\tvoid test_redis_lrange(redisContext *context){\n    \t\tredisReply *result = (redisReply *)redisCommand(context, \"lrange IMAGE_NAME_LIST 0 -1\");\n    \t\tredisReply **elements = result-\u003eelement;\n    \t\tsize_t size = result-\u003eelements;\n    \t\tfor(int i=0; i \u003c size; i++){\n        \t\tstring re = elements[i]-\u003estr;\n        \t\tint r = re.at(7) - 48;\n        \t\tcout \u003c\u003c \"r:\" \u003c\u003c r\u003c\u003cendl;\n    \t\t\t}\n    \t\tfreeReplyObject(result);\n    \t\tcout \u003c\u003c endl;\n\t\t}\n\n- 更多demo 参考[HiredisTest](https://github.com/Maicius/HiredisTest/blob/master/main.cpp)\n\n- 一点经验：由于关于hiredis的文档比较少，所以很多参数、返回值不得不自己进行测试，通过阅读源代码是一种方式，我比较喜欢的是通过jetbrain CLion的debug模式进行查看。如下图，在不清楚hiredis的lrange返回的参数的情况下，我通过在返回值那里设断点，通过添加监视等操作来判断如何正确获取返回值。并且，Clion是可以跨平台的，对学生免费。\n\n##### Python 使用redis\n\n- 这个非常简单，仅仅在这里仅提供一个[demo](https://github.com/Maicius/UniversityRecruitment-sSurvey/blob/master/jedis/jedis.py),这个demo是我自己写某个比较大的爬虫项目自己封装的一系列接口。\n\n##### Redis 踩坑笔记\n\n以前写的一篇博客，记录了Redis使用过程中的一些坑,欢迎交流\n[Redis 踩坑笔记](http://www.xiaomaidong.com/?p=308)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaicius%2Fhiredistest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaicius%2Fhiredistest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaicius%2Fhiredistest/lists"}