Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Actix from a Tokio App: mixing actix_web::main and tokio::main?

Currently I have a main written like the async example for the Reqwest library.

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {

We can use the exact example there for this. Now I want to basically add a -l <port> flag to alter the behavior of my application and when triggered in such a fashion, I want it to listen on the port and run a web server. I'm wanting to use Actix Web which is documented like this,

#[actix_web::main]
async fn main() -> std::io::Result<()> {

How can I synthesize two fn main: one decorated with #[actix_web::main] and one decorated with #[tokio::main] to use Actix Web from within an application that already uses Tokio? I can't find any documentation on this? How do we go about using Tokio stuff from an Actix Web server, and how do we port a Tokio application to an Actix Web application?

like image 733
NO WAR WITH RUSSIA Avatar asked Mar 01 '21 03:03

NO WAR WITH RUSSIA


People also ask

Does actix use Tokio?

Actix Web runs on Tokio, providing full 1 compatibility with its huge ecosystem of crates.

What is Tokio :: Main?

The #[tokio::main] function is a macro. It transforms the async fn main() into a synchronous fn main() that initializes a runtime instance and executes the async main function. The details of the Tokio runtime will be covered later.

What is Actix_rt?

Crate actix_rt. source · [−] Tokio-based single-threaded async runtime for the Actix ecosystem. In most parts of the the Actix ecosystem, it has been chosen to use !

Do you Tokio main on actix-web?

and in general don't tokio::main on actix-web when you can. actix-web has it's main macro for a very good reason. Sorry, something went wrong. Yup its not related to tokio version.

What is the difference between actix-web and run_in_Tokio?

actix-web would spawn a thread for listening sockets and distribute incoming streams to workers that all runs in it's own tokio current-thread runtime. It can inherit tokio current-thread runtime which is what run_in_tokio does. Use system::new is good practice and I don't get that API either. Sorry, something went wrong.

Can actix-web inherit a Tokio runtime?

actix-web cannot inherit a Tokio runtime, it will always create its own. Is that accurate? I care more about inheriting an existing Tokio runtime than I do about "extra threads." If you're saying that actix-web is going to run its own Tokio runtime anyways, then I'll just use System::new () and continue to wonder why run_in_tokio even exists.

What does actix-web actually do?

actix-web would spawn a thread for listening sockets and distribute incoming streams to workers that all runs in it's own tokio current-thread runtime. It can inherit tokio current-thread runtime which is what run_in_tokio does.


Video Answer


1 Answers

Since Actix-web internally uses Tokio, when you use #[actix_web::main], you will be able to use all the usual Tokio utilities just as if you had used #[tokio::main].

However be aware that you need to match up the versions of Tokio correctly. Actix-web version 3.x.y uses Tokio 0.2.x, so when using that version of Actix, you need to use utilities that work with that version of Tokio. To use the latest version of Tokio, you need to use the 4.0.0-beta.x versions of Actix-web instead.

Feel free to edit this answer once Actix-web 4 is out of beta.

like image 181
Alice Ryhl Avatar answered Oct 22 '22 17:10

Alice Ryhl