内容简介:版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lastsweetop/article/details/89375879
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lastsweetop/article/details/89375879
golang的 redis 端
要注意的是要先订阅回复,然后再发送请求。
package main
import (
"github.com/go-redis/redis"
)
var wait = make(chan interface{})
func main() {
client := redis.NewClient(&redis.Options{
Addr: "www.lastsweetop.com:6379",
Password: "", // no password set
DB: 0, // use default DB
})
pubsub := client.Subscribe("sayhello_rsp")
defer pubsub.Close()
pubsub.Receive();
go func() {
ch := pubsub.Channel();
select {
case channel := <-ch:
println(channel.Payload)
wait <- 1
}
}()
println("hello rust")
client.Publish("sayhello_req", "hello rust")
<-wait
}
rust作为被调用端订阅请求,rust将订阅到消息通知和处理分开,并且做成多线程处理。
use std::sync::mpsc;
use std::thread;
use std::sync::Arc;
use std::sync::Mutex;
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
let client = redis::Client::open("redis://www.lastsweetop.com/")?;
let mut con = client.get_connection()?;
let mut pubsub = con.as_pubsub();
pubsub.subscribe("sayhello_req")?;
let (tx, rx) = mpsc::channel();
let receiver = Arc::new(Mutex::new(rx));
for _ in 1..10 {
let con2 = client.get_connection()?;
let rx_arc = Arc::clone(&receiver);
thread::spawn(move || {
loop {
let message = rx_arc.lock().unwrap().recv().unwrap();
println!("payload : {}", message);
redis::cmd("PUBLISH").arg("sayhello_rsp").arg("hello go").execute(&con2);
}
});
}
loop {
let msg = pubsub.get_message()?;
let payload: String = msg.get_payload()?;
tx.send(payload)?;
}
}
当然反过来也一样,但是基本 go 做前端api业务,然后rust做具体的底层实现。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Python语言程序设计基础(第2版)
嵩天、礼欣、黄天羽 / 高等教育出版社 / 2017-2 / 39
本书提出了以理解和运用计算生态为目标的Python语言教学思想,不仅系统讲解了Python语言语法,同时介绍了从数据理解到图像处理的14个Python函数库,向初学Python语言的读者展示了全新的编程语言学习路径。 全书一共设计了25个非常具有现代感的实例,从绘制蟒蛇、理解天天向上的力量到机器学习、网络爬虫,从文本进度条、统计名著人物重要性到图像手绘效果、雷达图绘制,绝大多数实例为作者原创......一起来看看 《Python语言程序设计基础(第2版)》 这本书的介绍吧!