Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to spawn a task in a task #2991

Closed
YaaMe opened this issue Jul 2, 2019 · 11 comments

Comments

@YaaMe
Copy link

commented Jul 2, 2019

Depends on commit

/// Spawns a task in the background that runs the future passed as parameter.
pub fn spawn_task(&self, task: Box<dyn Future<Item = (), Error = ()> + Send>) {
let _ = self.to_spawn_tx.unbounded_send(task);
}

and use it like

None => {
service.spawn_task(Box::new(grandpa::run_grandpa_observer(
config,
link_half,
service.network(),
service.on_exit(),
)?));
},

but how to handle

// service_package/src/lib
pub fn get_task(to_spawn_tx: XXX) -> Task {
	// other code
	to_spawn_tx.unbounded_send(another_task);
	// other code
	my_pack::get_task()
}
// node/cli/service
service.spawn_task(Box::new(my_package::get_task(
	service.to_spawn_tx.clone() // <- private now
)))

I think return instance self.to_spawn_tx.clone() is necessary....or any other suggestions?

@bkchr

This comment has been minimized.

Copy link
Contributor

commented Jul 2, 2019

I don't understand your question? What do you want to achieve? A handle for spawning tasks?

@bkchr bkchr added the Z1-question label Jul 2, 2019

@kigawas

This comment has been minimized.

Copy link
Contributor

commented Jul 2, 2019

@bkchr
IMO he's trying to spawn a custom task, the way it spawning a task is to call self.to_spawn_tx.unbounded_send(task), but the self.to_spawn_tx is private so we cannot do it without changing substrate's code.

Is there something else we can do?

@bkchr

This comment has been minimized.

Copy link
Contributor

commented Jul 2, 2019

And you can not call service.spawn_task()?

@YaaMe

This comment has been minimized.

Copy link
Author

commented Jul 2, 2019

I want to inject a service layer with spawning some tasks.Depends on fn spawn_task.
the only way to handle it is like

service.spawn_task(Box::new(my_package::run_task(&service)));

1: service.to_spawn_tx is private
2: you couldn't clone service
3: how to define the service type in fn run_task(service: ?);
4: a bit wired..because package shouldn't touch service instance, just need bridge.

which seems not a good idea..

The another way to do this is bring them here

{
  // my other code
  service.spawn_task(my_task);
  // my other code
}

which seems worse...because it won't be a independent package any more..

@bkchr

This comment has been minimized.

Copy link
Contributor

commented Jul 2, 2019

So, would this: 2c06fb5 solve your problem?

If yes, I will make a pr out of it.

@YaaMe

This comment has been minimized.

Copy link
Author

commented Jul 2, 2019

yep

@bkchr

This comment has been minimized.

Copy link
Contributor

commented Jul 2, 2019

Okay, than give me moment :)

@bkchr

This comment has been minimized.

Copy link
Contributor

commented Jul 2, 2019

Here is the pr:#2992

@kigawas

This comment has been minimized.

Copy link
Contributor

commented Jul 2, 2019

@bkchr So efficient! 🤣

@bkchr bkchr closed this in #2992 Jul 2, 2019

@tomaka

This comment has been minimized.

Copy link
Member

commented Jul 2, 2019

I would strongly suggest refactoring the code to be poll-based instead, but I can't help much without looking at code.

@bkchr

This comment has been minimized.

Copy link
Contributor

commented Jul 2, 2019

What you mean by poll-based?
If you send your future to service isn't it poll-based?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.