✨ worker
This commit is contained in:
parent
5cb4facc48
commit
e3393f1e09
11 changed files with 841 additions and 48 deletions
21
src/job.rs
21
src/job.rs
|
@ -1,9 +1,10 @@
|
|||
use std::sync::{Arc, Mutex, mpsc};
|
||||
use crossbeam::channel::{Receiver, Sender};
|
||||
use std::sync::mpsc;
|
||||
|
||||
#[derive(Clone)]
|
||||
/// A generic job dispatcher struct that allows sending jobs of type `T` and receiving results of type `V` using message passing.
|
||||
pub struct JobDispatcher<T: Send + 'static, V: Send + 'static> {
|
||||
sender: Arc<Mutex<mpsc::Sender<JobOrder<T, V>>>>,
|
||||
sender: Sender<JobOrder<T, V>>,
|
||||
}
|
||||
|
||||
impl<T: Send + 'static, V: Send + 'static> JobDispatcher<T, V> {
|
||||
|
@ -27,14 +28,10 @@ impl<T: Send + 'static, V: Send + 'static> JobDispatcher<T, V> {
|
|||
/// assert_eq!(result, 4);
|
||||
/// ```
|
||||
#[must_use]
|
||||
pub fn new() -> (Self, mpsc::Receiver<JobOrder<T, V>>) {
|
||||
let (sender, receiver) = mpsc::channel();
|
||||
(
|
||||
Self {
|
||||
sender: Arc::new(Mutex::new(sender)),
|
||||
},
|
||||
receiver,
|
||||
)
|
||||
pub fn new() -> (Self, Receiver<JobOrder<T, V>>) {
|
||||
let (sender, receiver) = crossbeam::channel::bounded(12);
|
||||
|
||||
(Self { sender: sender }, receiver)
|
||||
}
|
||||
|
||||
/// Sends a job of type `T` to the job dispatcher and waits for its result of type `V`.
|
||||
|
@ -47,7 +44,7 @@ impl<T: Send + 'static, V: Send + 'static> JobDispatcher<T, V> {
|
|||
let job_order = JobOrder::new(param, move |ret| {
|
||||
tx.send(ret).unwrap();
|
||||
});
|
||||
self.sender.lock().unwrap().send(job_order).unwrap();
|
||||
self.sender.send(job_order).unwrap();
|
||||
rx.recv().unwrap()
|
||||
}
|
||||
|
||||
|
@ -58,7 +55,7 @@ impl<T: Send + 'static, V: Send + 'static> JobDispatcher<T, V> {
|
|||
let job_order = JobOrder::new(param, move |ret| {
|
||||
tx.send(ret).unwrap();
|
||||
});
|
||||
self.sender.lock().ok()?.send(job_order).ok()?;
|
||||
self.sender.send(job_order).ok()?;
|
||||
rx.recv().ok()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue