On Monday, 31 December, 2018 13:19, Jesse Rittner <[email protected]>
wrote:
>Keith Medcalf wrote
>> What are you trying to accomplish? Perhaps what you really want is
>> a progress callback?
> I'm trying to write a function to run a query with a timeout. If the
> timeout expires, the query must stop execution within a "reasonable" amount
> of time.
> To use a progress callback, it sounds like I'd have to choose a small
> enough N and poll some kind of "timed out" flag, which sounds undesirable.
> If instead of sqlite3_interrupt, we had sqlite3_begin_interrupt and
> sqlite3_end_interrupt, that would meet my needs a lot better. Then I
> would
> just do the following:
> 1. Have thread 1 call sqlite3_step.
> 2. Have thread 2 sleep for whatever timeout, then call
> sqlite3_begin_interrupt.
> 3. Once sqlite3_step returns, have thread 1 signal thread 2.
> 4. Have thread 2 call sqlite3_end_interrupt.
> Then there would never be a race condition because the interrupt
> remains in effect even while there are no running statements.
> Alternatively, having some sort of object to pass into sqlite3_step
> would also work, as then I could just call some sort of cancel method on
> that object and have it interrupt that call only.
If the timeout is so short that you need to make sure that a query is actively
running, then simply wait for it it be running if necessary:
def interrupt_function(db, stmt, timeout, whizround)
while whizround and !sqlite3_stmt_busy /* whizround waiting for
statement to start */
sleep(0.001)
sleep(timeout) /* wait for our timeout */
if sqlite3_stmt_busy(stmt) /* if statement is running */
sqlite3_interrupt(db) /* interrupt it */
def run_query_with_timeout(db, query, timeout, whizround)
stmt = prepare(db, query)
create_thread A interrupt_function(db, stmt, timeout, whizround)
while sqlite3_step(stmt) == SQLITE_ROW
... process the row ...
cancel_thread A
join_thread A /* make sure the thread is ended */
sqlite3_finalize(stmt)
---
The fact that there's a Highway to Hell but only a Stairway to Heaven says a
lot about anticipated traffic volume.
_______________________________________________
sqlite-users mailing list
[email protected]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users