mirror of
https://github.com/4yn/slidershim.git
synced 2025-02-02 04:27:58 +01:00
better async
This commit is contained in:
parent
281e4b02b1
commit
7f155903ca
12
src-tauri/Cargo.lock
generated
12
src-tauri/Cargo.lock
generated
@ -105,6 +105,17 @@ dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "async-trait"
|
||||
version = "0.1.52"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "061a7acccaa286c011ddc30970520b98fa40e00c9d644633fb26b5fc63a265e3"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "atk"
|
||||
version = "0.14.0"
|
||||
@ -3041,6 +3052,7 @@ checksum = "9def91fd1e018fe007022791f865d0ccc9b3a0d5001e01aabb8b40e46000afb5"
|
||||
name = "slidershim"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"directories",
|
||||
"env_logger",
|
||||
"futures",
|
||||
|
@ -30,6 +30,7 @@ palette = "0.6.0"
|
||||
winapi = "0.3.9"
|
||||
|
||||
futures = "0.3.19"
|
||||
async-trait = "0.1.52"
|
||||
tokio = { version="1.16.1", features=["rt-multi-thread","macros"] }
|
||||
hyper = { version="0.14.16", features=["server","http1","http2","tcp"] }
|
||||
tungstenite = { version="0.16.0", default-features=false }
|
||||
|
@ -1,39 +1,45 @@
|
||||
extern crate slidershim;
|
||||
|
||||
use std::{io, time::Duration};
|
||||
use async_trait::async_trait;
|
||||
use std::{future::Future, io, time::Duration};
|
||||
|
||||
use tokio::time::sleep;
|
||||
use tokio::{select, time::sleep};
|
||||
|
||||
// use slidershim::slider_io::worker::{AsyncJob, AsyncJobFut, AsyncJobRecvStop, AsyncWorker};
|
||||
use slidershim::slider_io::worker::{AsyncJob, AsyncWorker};
|
||||
|
||||
// struct CounterJob;
|
||||
struct CounterJob;
|
||||
|
||||
// impl AsyncJob for CounterJob {
|
||||
// fn job(self, mut recv_stop: AsyncJobRecvStop) -> AsyncJobFut {
|
||||
// return Box::pin(async move {
|
||||
// let mut x = 0;
|
||||
// loop {
|
||||
// x += 1;
|
||||
// println!("{}", x);
|
||||
// sleep(Duration::from_millis(500)).await;
|
||||
// match recv_stop.try_recv() {
|
||||
// Ok(_) => {
|
||||
// println!("@@@");
|
||||
// break;
|
||||
// }
|
||||
// _ => {}
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
#[async_trait]
|
||||
impl AsyncJob for CounterJob {
|
||||
async fn do_work<F: Future<Output = ()> + Send>(self, stop_signal: F) {
|
||||
let job_a = async {
|
||||
println!("Start job A");
|
||||
let mut x = 0;
|
||||
loop {
|
||||
x += 1;
|
||||
println!("{}", x);
|
||||
sleep(Duration::from_millis(100)).await;
|
||||
}
|
||||
};
|
||||
let job_b = async move {
|
||||
println!("Start job B");
|
||||
stop_signal.await;
|
||||
println!("Stop signal hit at job B");
|
||||
};
|
||||
|
||||
select! {
|
||||
_ = job_a => {},
|
||||
_ = job_b => {},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
env_logger::Builder::new()
|
||||
.filter_level(log::LevelFilter::Debug)
|
||||
.init();
|
||||
|
||||
// let worker = AsyncWorker::new("counter", CounterJob);
|
||||
let worker = AsyncWorker::new("counter", CounterJob);
|
||||
let mut input = String::new();
|
||||
let string = io::stdin().read_line(&mut input).unwrap();
|
||||
}
|
||||
|
17
src-tauri/src/bin/test_brokenithm.rs
Normal file
17
src-tauri/src/bin/test_brokenithm.rs
Normal file
@ -0,0 +1,17 @@
|
||||
extern crate slidershim;
|
||||
|
||||
use std::{io, time::Duration};
|
||||
|
||||
use tokio::time::sleep;
|
||||
|
||||
// use slidershim::slider_io::{brokenithm::BrokenithmJob, worker::AsyncWorker};
|
||||
|
||||
fn main() {
|
||||
env_logger::Builder::new()
|
||||
.filter_level(log::LevelFilter::Debug)
|
||||
.init();
|
||||
|
||||
// let worker = AsyncWorker::new("brokenithm", BrokenithmJob);
|
||||
let mut input = String::new();
|
||||
let string = io::stdin().read_line(&mut input).unwrap();
|
||||
}
|
@ -9,6 +9,8 @@ use hyper::{
|
||||
Body, Request, Response, Server,
|
||||
};
|
||||
|
||||
// use crate::slider_io::worker::{AsyncJob, AsyncJobFut, AsyncJobRecvStop};
|
||||
|
||||
async fn handle_request(
|
||||
request: Request<Body>,
|
||||
remote_addr: SocketAddr,
|
||||
@ -19,7 +21,7 @@ async fn handle_request(
|
||||
))))
|
||||
}
|
||||
|
||||
pub async fn brokenithm_server() {
|
||||
async fn brokenithm_server() {
|
||||
let addr = SocketAddr::from(([0, 0, 0, 0], 1666));
|
||||
|
||||
info!("Brokenithm opening on {:?}", addr);
|
||||
@ -38,3 +40,11 @@ pub async fn brokenithm_server() {
|
||||
eprintln!("Server error: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
// struct BrokenithmJob;
|
||||
|
||||
// impl AsyncJob {
|
||||
// fn job(self, mut recv_stop: AsyncJobRecvStop) -> AsyncJobFut {
|
||||
// return Box::pin()
|
||||
// }
|
||||
// }
|
||||
|
@ -1,3 +1,5 @@
|
||||
use async_trait::async_trait;
|
||||
use log::info;
|
||||
use std::{
|
||||
future::Future,
|
||||
pin::Pin,
|
||||
@ -8,8 +10,6 @@ use std::{
|
||||
thread,
|
||||
};
|
||||
|
||||
use log::info;
|
||||
|
||||
use tokio::{
|
||||
runtime::Runtime,
|
||||
sync::oneshot::{self, Receiver},
|
||||
@ -66,10 +66,9 @@ impl Drop for ThreadWorker {
|
||||
}
|
||||
}
|
||||
|
||||
pub type AsyncJobRecvStop = oneshot::Receiver<()>;
|
||||
pub type AsyncJobFut = Pin<Box<dyn Future<Output = ()> + Send + 'static>>;
|
||||
pub trait AsyncJob {
|
||||
fn job(self, recv_stop: AsyncJobRecvStop) -> AsyncJobFut;
|
||||
#[async_trait]
|
||||
pub trait AsyncJob: Send + 'static {
|
||||
async fn do_work<F: Future<Output = ()> + Send>(self, stop_signal: F);
|
||||
}
|
||||
|
||||
pub struct AsyncWorker {
|
||||
@ -80,7 +79,10 @@ pub struct AsyncWorker {
|
||||
}
|
||||
|
||||
impl AsyncWorker {
|
||||
pub fn new<T: 'static + AsyncJob + Send>(name: &'static str, job: T) -> AsyncWorker {
|
||||
pub fn new<T>(name: &'static str, job: T) -> AsyncWorker
|
||||
where
|
||||
T: AsyncJob,
|
||||
{
|
||||
info!("Async worker starting {}", name);
|
||||
|
||||
let (send_stop, recv_stop) = oneshot::channel::<()>();
|
||||
@ -91,8 +93,11 @@ impl AsyncWorker {
|
||||
.unwrap();
|
||||
|
||||
let task = runtime.spawn(async move {
|
||||
let fut = job.job(recv_stop);
|
||||
fut.await;
|
||||
job
|
||||
.do_work(async move {
|
||||
recv_stop.await;
|
||||
})
|
||||
.await;
|
||||
});
|
||||
|
||||
AsyncWorker {
|
||||
|
Loading…
x
Reference in New Issue
Block a user