-
Notifications
You must be signed in to change notification settings - Fork 377
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
Transactions don't work with async/await #697
Comments
Hmm. In general, That said, I do think usability in async contexts is important to support, so I'll look into this. |
@thomcc that makes sense! |
Are you able to use It would be nice to fix the general problem though -- I suspect fixing that would mean many other APIs don't work still. |
But what point to use blocking code (all sqlite code is blocking) in async context? |
Not using |
(This also roughly relates the question raised at #188 (comment) the other day). From some discussion, I've heard that doing blocking operations on async worker threads is a big problem, and that it's probably good that rusqlite behave as it does currently, as a way to signal to users that they're doing something very wrong. In the mean time, you should either follow @xfix's advice (use Eventually there's a reasonable chance that similar async support will become desirable for rusqlite (I suspect Firefox may eventually need it...), but there are no short term plans for this. (And if this happens it wouldn't be the default though, it would be behind in a feature; or possibly even a separate Because of all that I'm going to close this as works-as-intended for now, even if it's a bit surprising. |
Worth noting that sqlx might be a better choice if you need to use sqlite and async. It's somewhat deliberate that rusqlite is awkward to use in this context, as blocking IO is bad here. (At least, that's what people more experienced with async tell me is good to do) |
This is correct, SQLite is not thread-safe, connections cannot be shared with other threads. Make your connection a thread local or use a mutex. |
Currently there doesn't seem to be any good way to use SQLite in async Rust. |
Do you have any suggestions? |
I'm attempting to create async-wrapper for This would fix the main problem of This would also allow running async operations during transaction:
|
That sounds cool. If you need something concrete to make stuff easier for that use case, feel free to file issues. |
https://www.sqlite.org/asyncvfs.html A async C FFI was added to SQLite, but did not offer much performance gains over using Maybe it would be OK for your code just to sync wait until the call returns. Assuming the DB file is in WAL mode (so that |
It doesn't seem like calling |
But if you have a single threaded event loop there is not any point, as the SQLite function blocks it for the same amount of time - no difference between blocking it now for x or blocking it later for x? |
Right — if you only have one thread, SQLite will block it unless you use an async VFS. I was considering the case where you push the SQLite transaction to a separate I'm curious what your use case is, though, where you don't have threads available. Is this an embedded thing? |
Async rusqlite built on top of rusqlite. |
I'm migrating a pretty big codebase to use async/await and ran into problem with making rusqlite work.
I understand why
Connection
is notSync
and normally i would use aMutex
to hold a connection in a multi threaded environment, but in this case i'm holding a reference to aConnection
and&T: Send
is notSend
.playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=c25f81c48a319e5fbaf88b79ac1989eb
The text was updated successfully, but these errors were encountered: