Jelajahi Sumber

script/research/consensusd/src/main.rs: testing parallel tasks.

aggstam 4 tahun lalu
induk
melakukan
df822d20aa
1 mengubah file dengan 34 tambahan dan 2 penghapusan
  1. 34 2
      script/research/consensusd/src/main.rs

+ 34 - 2
script/research/consensusd/src/main.rs

@@ -1,5 +1,6 @@
 use std::net::SocketAddr;
 use std::net::SocketAddr;
 
 
+use easy_parallel::Parallel;
 use async_executor::Executor;
 use async_executor::Executor;
 use async_std::sync::Arc;
 use async_std::sync::Arc;
 use clap::{IntoApp, Parser};
 use clap::{IntoApp, Parser};
@@ -48,7 +49,7 @@ pub struct CliConsensusd {
 }
 }
 
 
 /// Consensus service initialization.
 /// Consensus service initialization.
-async fn start(executor: Arc<Executor<'_>>, config: ConsensusdConfig) -> Result<()> {
+async fn start(executor: Arc<Executor<'_>>, config: &ConsensusdConfig) -> Result<()> {
     let server_config = RpcServerConfig {
     let server_config = RpcServerConfig {
         socket_addr: config.rpc_listen_address,
         socket_addr: config.rpc_listen_address,
         use_tls: config.serve_tls,
         use_tls: config.serve_tls,
@@ -64,6 +65,14 @@ async fn start(executor: Arc<Executor<'_>>, config: ConsensusdConfig) -> Result<
     listen_and_serve(server_config, chain_service, executor).await
     listen_and_serve(server_config, chain_service, executor).await
 }
 }
 
 
+async fn start2(executor: Arc<Executor<'_>>, config: &ConsensusdConfig) -> Result<()> {
+    
+    while true {
+        println!("sss");
+    };
+    Ok(())
+}
+
 const CONFIG_FILE_CONTENTS: &[u8] = include_bytes!("../consensusd_config.toml");
 const CONFIG_FILE_CONTENTS: &[u8] = include_bytes!("../consensusd_config.toml");
 
 
 /// Consensus daemon initialization.
 /// Consensus daemon initialization.
@@ -82,5 +91,28 @@ async fn main() -> Result<()> {
     let config: ConsensusdConfig = Config::<ConsensusdConfig>::load(config_path)?;
     let config: ConsensusdConfig = Config::<ConsensusdConfig>::load(config_path)?;
 
 
     let ex = Arc::new(Executor::new());
     let ex = Arc::new(Executor::new());
-    smol::block_on(ex.run(start(ex.clone(), config)))
+    let ex2 = ex.clone();
+    let ex3 = ex.clone();
+    let (signal, shutdown) = async_channel::unbounded::<()>();
+    let signal1 = signal.clone();
+    let signal2 = signal.clone();    
+    let (result, _) = Parallel::new()
+        .add(|| {
+            smol::future::block_on(async {
+                start(ex2, &config).await?;
+                drop(signal1);
+                Ok::<(), darkfi::Error>(())
+            })
+        })
+        .add(|| {
+            smol::future::block_on(async {
+                start2(ex3, &config).await?;
+                drop(signal2);
+                Ok::<(), darkfi::Error>(())
+            })
+        })
+        // Run the main future on the current thread.
+        .finish(|| smol::future::block_on(ex.run(shutdown.recv())));
+
+    Ok(())
 }
 }