有没有办法中止SQLite调用? [英] Is there a way to abort an SQLite call?

查看:169
本文介绍了有没有办法中止SQLite调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows应用程序中使用SQLite3。我有源代码(所谓的SQLite合并)。



有时我必须执行重查询。也就是说,我在准备的语句上调用 sqlite3_step ,并且完成(由于重的I / O负载)需要很多时间。



我想知道是否有可能中止这样的调用。我也很高兴如果有能力在同一线程中调用的中间进行一些后台处理(因为大多数时间花在等待I / O完成) 。



我想过自己修改SQLite代码。在最简单的情况下,我可以在每次调用 ReadFile / WriteFile ,并相应地返回错误代码。为了允许后台处理,文件应以重叠模式打开(这使得异步 ReadFile / WriteFile )。



有可能中断 WriteFile 在某些情况下可能会使数据库处于不一致状态,即使启用日志?我想不是,因为日志文件的整个想法是准备任何错误的任何种类。



另外,有人试过类似的东西吗?



提前感谢。



编辑:



感谢ereOn。我不知道 sqlite3_interrupt 的存在。这可能回答了我的问题。



现在,对于所有想知道如何(以及为什么)期望在同一个线程中的I / O期间做一些后台处理。



很遗憾,很多人都不熟悉所谓的重叠I / O。



http://en.wikipedia.org/wiki/Overlapped_I/O



使用它以异步发出I / O操作,调用线程未阻止。然后,使用完成机制之一接收I / O完成状态:等待事件,新例程排队到APC或完成端口。



使用这种技术不必创建额外的线程。实际上,创建线程的唯一真实合法性是当你的瓶颈是计算时间(即CPU负载),并且机器有几个CPU(或核心)。



并且创建一个线程只是让它被操作系统大多数时候阻止 - 这没有意义。这导致OS资源的不合理浪费,使程序复杂化(需要同步等)。



不幸的是,并不是所有的库/ API都允许异步操作模式



I



对于所有那些坚持认为它不值得做的事情在后台而等待的I / O已经找到解决方案, O完成使用重叠I / O。我不同意,我认为没有必要争论这一点。至少这与主题无关。



我是一个Windows程序员(你可能会注意到),我有一个非常丰富的经验,在各种多任务。加上我也是一个司机作家,所以我也知道事情如何工作幕后。



我知道这是一个常见的做法线程做几件事并行。但这并不意味着这是一个好的做法。请允许我不要遵循惯例。

解决方案

我不明白为什么要中断来自同一个线程,我甚至不明白如何可能:如果当前线程被阻塞,等待一些IO,你不能执行任何其他代码。 (是的,这就是被阻止的意思)



也许如果你给我们更多的提示,为什么你想要这个,我们可能会帮助进一步。



通常,我使用 sqlite3_interrupt() 取消通话。但是,这显然涉及到从另一个线程调用。


I'm using SQLite3 in a Windows application. I have the source code (so-called SQLite amalgamation).

Sometimes I have to execute heavy queries. That is, I call sqlite3_step on a prepared statement, and it takes a lot of time to complete (due to the heavy I/O load).

I wonder if there's a possibility to abort such a call. I would also be glad if there was an ability to do some background processing in the middle of the call within the same thread (since most of the time is spent in waiting for the I/O to complete).

I thought about modifying the SQLite code myself. In the simplest scenario I could check some condition (like an abort event handle for instance) before every invocation of either ReadFile/WriteFile, and return an error code appropriately. And in order to allow the background processing the file should be opened in the overlapped mode (this enables asynchronous ReadFile/WriteFile).

Is there a chance that interruption of WriteFile may in some circumstances leave the database in the inconsistent state, even with the journal enabled? I guess not, since the whole idea of the journal file is to be prepared for any error of any kind. But I'd like to hear more opinions about this.

Also, did someone tried something similar?

Thanks in advance.

EDIT:

Thanks to ereOn. I wasn't aware of the existence of sqlite3_interrupt. This probably answers my question.

Now, for all of you who wonders how (and why) one expects to do some background processing during the I/O within the same thread.

Unfortunately not many people are familiar with so-called "Overlapped I/O".

http://en.wikipedia.org/wiki/Overlapped_I/O

Using it one issues an I/O operation asynchronously, and the calling thread is not blocked. Then one receives the I/O completion status using one of the completion mechanisms: waitable event, new routine queued into the APC, or the completion port.

Using this technique one doesn't have to create extra threads. Actually the only real legitimation for creating threads is when your bottleneck is the computation time (i.e. CPU load), and the machine has several CPUs (or cores).

And creating a thread just to let it be blocked by the OS most of the time - this doesn't make sense. This leads to the unjustified waste of the OS resources, complicates the program (need for synchronization and etc.).

Unfortunately not all the libraries/APIs allow asynchronous mode of operation, thus making creating extra threads the necessarily evil.

EDIT2:

I've already found the solution, thansk to ereOn.

For all those who nevertheless insist that it's not worth doing things "in background" while "waiting" for the I/O to complete using overlapped I/O. I disagree, and I think there's no point to argue about this. At least this is not related to the subject.

I'm a Windows programmer (as you may noticed), and I have a very extensive experience in all kinds of multitasking. Plus I'm also a driver writer, so that I also know how things work "behind the scenes".

I know that it's a "common practice" to create several threads to do several things "in parallel". But this doesn't mean that this is a good practice. Please allow me not to follow the "common practice".

解决方案

I don't understand why you want the interruption to come from the same thread and I even don't understand how that would be possible: if the current thread is blocked, waiting for some IO, you can't execute any other code. (Yeah, that's what "blocked" means)

Perhaps if you give us more hints about why you want this, we might help further.

Usually, I use sqlite3_interrupt() to cancel calls. But this, obviously, involves that the call is made from another thread.

这篇关于有没有办法中止SQLite调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆