I've always been curious about tokio and async trait

Because I have also seen the usage scenarios of tokio introduced on the official website, but I want to know whether I can to use it. For example, when I insert 5000 or 500000 pieces of data into the database, I will establish a connection with the database and then execute SQL. tokio is some operations for non blocking IO. If I add it, will it prompt my insertion speed? What will happen to it in the case of single thread or multi thread?

Should I add tokio or async-trait dependencies for my crates :thought_balloon:

If I'm not dealing with the network and I'm writing an embedded device and virtual machine, I don't think I'll use it.

Think of async in general as being able to better use a hardware thread, when what you're doing would otherwise be blocking on something external like a network connection.

This is in theory a good match for an embedded device doing a external sql connection, if it's an external, network, database connection and you're able to run multiple queries at the same time. Without those conditions, it won't be nearly as helpful, but probably won't hurt much either.

Performance-wise, expect async code to be at worst about 2x the local costs of the single threaded sync version, not counting time outside the process, eg actually waiting for the network to come back. fs is generally an exception and can be much slower, due to working around historical limitations with async file IO in Linux.

The trade off is you can run multiple of those at once, and it can be much more efficient than using hardware threads in terms of memory, as only the data specifically needed for continuing is kept in memory, rather than the whole stack, and switching contexts doesn't require a kernel transition.

Depending on just how embedded you are, tokio might be too heavy in pure code size, but that seems pretty unlikely if you're making sql requests to me.

Tldr: Give it a try, you are likely to be pleasantly surprised.

2 Likes

thanks

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.