-
Notifications
You must be signed in to change notification settings - Fork 2.6k
use ThreadPool to execute spawn_worker(fn) #3836
Changes from 6 commits
bbd6e42
df4d06d
9c87d8b
b7f48f1
6394319
742d72f
d09f65d
48899be
e5a0bd1
8fedb51
1114fa5
07cb32d
a37fc79
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,8 @@ use std::{ | |
| sync::Arc, | ||
| }; | ||
|
|
||
| extern crate num_cpus; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should not be needed in 2018 edition?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sorry I don't understand what you mean.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The usage of
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, just remove that line, fix the tests and we are good to go.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are build results below the comments section on github: Actually it's not a test failing, but you need to run
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I see.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have committed the change of Cargo.lock . But there is a trouble, when I run 'cargo check' in the path ' ~/substrate/ ' , it shows that futures-io-preview can't compile.
error[E0106]: missing lifetime specifier error[E0106]: missing lifetime specifier error[E0106]: missing lifetime specifier error[E0106]: missing lifetime specifier error[E0106]: missing lifetime specifier error: aborting due to 6 previous errors
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But your code seem to be compiling well on the CI machine.. Make sure you have the latest version of rustc and stable/nightly toolchains installed.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is my rust version. rustc 1.38.0-nightly (69656fa4c 2019-07-13) |
||
| use threadpool::ThreadPool; | ||
| use client::runtime_api::ApiExt; | ||
| use futures::future::Future; | ||
| use log::{debug, warn}; | ||
|
|
@@ -58,6 +60,7 @@ pub struct OffchainWorkers<Client, Storage, Block: traits::Block> { | |
| client: Arc<Client>, | ||
| db: Storage, | ||
| _block: PhantomData<Block>, | ||
| thread_pool: ThreadPool, | ||
| } | ||
|
|
||
| impl<Client, Storage, Block: traits::Block> OffchainWorkers<Client, Storage, Block> { | ||
|
|
@@ -67,6 +70,7 @@ impl<Client, Storage, Block: traits::Block> OffchainWorkers<Client, Storage, Blo | |
| client, | ||
| db, | ||
| _block: PhantomData, | ||
| thread_pool: ThreadPool::new(num_cpus::get()), | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -116,7 +120,7 @@ impl<Client, Storage, Block> OffchainWorkers< | |
| debug!("Spawning offchain workers at {:?}", at); | ||
| let number = *number; | ||
| let client = self.client.clone(); | ||
| spawn_worker(move || { | ||
| self.spawn_worker(move || { | ||
| let runtime = client.runtime_api(); | ||
| let api = Box::new(api); | ||
| debug!("Running offchain workers at {:?}", at); | ||
|
|
@@ -134,19 +138,18 @@ impl<Client, Storage, Block> OffchainWorkers< | |
| futures::future::Either::Right(futures::future::ready(())) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Spawns a new offchain worker. | ||
| /// | ||
| /// We spawn offchain workers for each block in a separate thread, | ||
| /// since they can run for a significant amount of time | ||
| /// in a blocking fashion and we don't want to block the runtime. | ||
| /// | ||
| /// Note that we should avoid that if we switch to future-based runtime in the future, | ||
| /// alternatively: | ||
| /// TODO [ToDr] (#1458) we can consider using a thread pool instead. | ||
| fn spawn_worker(f: impl FnOnce() -> () + Send + 'static) { | ||
| std::thread::spawn(f); | ||
| /// Spawns a new offchain worker. | ||
| /// | ||
| /// We spawn offchain workers for each block in a separate thread, | ||
| /// since they can run for a significant amount of time | ||
| /// in a blocking fashion and we don't want to block the runtime. | ||
| /// | ||
| /// Note that we should avoid that if we switch to future-based runtime in the future, | ||
| /// alternatively: | ||
| fn spawn_worker(&self, f: impl FnOnce() -> () + Send + 'static) { | ||
| self.thread_pool.execute(f); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.