Home > Software engineering >  How to use async/await for not-async io-operation? such as get/put in leveldb
How to use async/await for not-async io-operation? such as get/put in leveldb

Time:09-07

I want to use async/await for database, but I don't know how to do it.

And I found that mongodb has a async/await driver based on async conections.

But levelDB is not async.

What should I do?

I read the tokio document, may be I can use spawn_blocking to run a async io-task.

Is it right? If I frequently use spawn_blocking, will it affect the performance?

CodePudding user response:

The spawn_blocking part of the Tokio API uses a separate thread pool (to the core threads used for async tasks) with a configurable keep-alive time if threads are unused. So you may incur the overhead of spawning a new system thread. If you're always performing the same blocking tasks, a possible optimization could be to use a channel to communicate between async tasks and a single long running sync task which retrieves messages containing the parameters from the channel.

  • Related