Crates.io | mp2c |
lib.rs | mp2c |
version | 0.1.2 |
source | src |
created_at | 2020-09-14 11:12:45.245008 |
updated_at | 2020-11-18 07:02:53.916891 |
description | A multi producer multi polling consumer library that enables multiple producers to send messages to multiple consumers completely asynchronously. |
homepage | |
repository | |
max_upload_size | |
id | 288554 |
size | 13,354 |
Multi producer multi polling consumer
MP2C is a data structure that enables multiple producers/publishers to send messages to multiple consumers/subscribers.
The async mp2c data structure is called a Carousel
.
A message
in mp2c
context is a vector of u8
. It's up to the producers/ consumers to marshall and unmarshall these messages as they best see fit.
Yes.
Cloning a mp2c::asynch::Carousel
creates a clone of the underlying std::sync::Sender
and every invocation of Carousel::put
will send messages to the consumers.
mp2c::asynch::Carousel
supports full async behavior. All messages put on the Carousel
are asynchronously sent to the consumers.
Yes. In the spirit of don't communicate by sharing memory, share memory by communicating, all messages are cloned as many times as the count of mp2c::asynch::Consumer
s.
use mp2c::asynch::{Carousel, Consumer};
struct TestConsumer1;
impl Consumer for TestConsumer1 {
fn consume(&mut self, data: Vec<u8>) {
let msg = String::from_utf8(data).unwrap();
// do something with msg
}
}
struct TestConsumer2;
impl Consumer for TestConsumer2 {
fn consume(&mut self, data: Vec<u8>) {
let msg = String::from_utf8(data).unwrap();
// do something with msg
}
}
let mut v: Vec<Box<dyn Consumer + Send + 'static>> = Vec::new();
v.push(Box::new(TestConsumer1));
v.push(Box::new(TestConsumer2));
let c = Carousel::new(v);
for _ in 1..10 {
let cloned_c = c.clone();
let t = std::thread::spawn(move || {
cloned_c.put(String::from("test").into_bytes());
});
t.join().unwrap();
}
Add a message id to each message being put on the data carousel.
Make consumer mutable
Updated README with example.
Initial release.