On Thu, 2 Feb 2006, Lodewijk Duymaer van Twist wrote:
>Hi,
>
>
>
>I would like to create some sort of queue using sqlite. I'm thinking about
>transmitting commands to hardware devices/managers using a queue table.
>
>Is it possible to trigger a function created with sqlite3_create_function in
>one application by another application. Kind of like a plug-in application.
No. The function is associated with the database connection, and is
invoked by the sqlite VM of the compiled statement.
>
>
>
>Is this possible without polling the content of this queue table.
Question is, what's specifically wrong with polling?
Use an auto-increment integer primary key, then the queueing application
queues jobs by inserting them into the table (using NULL for the primary
key.)
The second application sits in a loop, reading the entries as they are
created using the primary auto-increment key to keep track of what has
already been processed. Pseudo code:
App 1:
void submit_job( args )
{
exec("insert into spool values (<args>);");
}
App 2:
boolean process_jobs()
{
static int last_job = 0;
int prev_last_job = last_job;
// Get jobs to be processed into separate table
exec("begin");
exec("insert into temp_spool select * from spool where id>$last_job");
exec("delete from spool where id>$last_job");
exec("commit;");
// Process jobs from separate table, then delete.
exec("begin");
foreach("select * from temp_spool;") {
last_job = id;
process job;
}
exec("delete from temp_spool;");
exec("commit;");
return ( last_job != prev_last_job );
}
In App2, process_jobs will return true if any jobs were processed, and
false otherwise. You'd test this return value and re-call process_jobs if
true as more jobs may have come in as you processed the last jobs:
App2:
while(1) {
if (!process_jobs) {
sleep(1);
}
}
This ensures you don't poll excessively when there ae no jobs coming, and
don't sleep unnecassarily when jobs are coming in steadily. You'd want the
temp_spool table in a seperate database, so as not to tie up the main
database while processing jobs.
Christian
--
/"\
\ / ASCII RIBBON CAMPAIGN - AGAINST HTML MAIL
X - AGAINST MS ATTACHMENTS
/ \