0

i have an asynchronous task that i need to execute but it appears that substrate node already has a runtime for doing that, if i simply make main function in node/main.rs asynchronous like this:

#[tokio::main]
async fn main() -> Result<(),sc_cli::Error>{ 
    tokio::spawn(myasyncFunction);
    command::run()
}

it gives me this error:

Thread 'main' panicked at 'Cannot start a runtime from within a runtime. This happens because a function (like `block_on`) attempted to block the current thread while the thread is being used to drive asynchronous tasks

can someone explain to me the error and what is the right place to add my async tasks in node?

1 Answer 1

1

If you want to execute your own asynchronous task, service.rs is the right place to trigger your functionality.

If we go with the flow, main() function is calling the command::run(), which actually parses and runs the command line args (if you specify any), and by default, it will go to the none case, which builds the new service for the full client.

None => {
        let runner = cli.create_runner(&cli.run)?;
        runner.run_node_until_exit(|config| async move {
            service::new_full(config).map_err(sc_cli::Error::Service)
        })
    },

Inside this new_full() function you can spawn your task in the task_manager like this:

task_manager.spawn_handle().spawn("name", "group_name", myasyncFunction);

The error specifies that the current thread is already set to derive the asynchronous tasks so we cannot block that thread by adding any other calls. So, after initializing the flow, inside the node->service.rs we can trigger any asynchronous tasks.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.