2

I want to make a program that polls a SQL database every n seconds (n>10). And it validates the data in a particular column and calls a function based on it.

For eg,

when column has '1' -> call function1  
when column has '2' -> call function2  
when column has '3' -> exits 

I looked into multithreading and saw a solution based on the threading.timer, will that satisfy my use-case?
Also, I will be okay with time drifts of up to +/-1 second.

1 Answer 1

1

Depending on the complexity of your query and the functions you call you may not need multiple threads. If your query runs fast and your function calls are cheap then just use the simplest solution that does the job, i.e. a loop which does the polling, calls your function and then sleeps for the remaining time.
If you want to call it every 10 seconds and your query took 1 sec and the function call took 1 sec then sleep for 8 secs so your time drift does not grow over time.

Sign up to request clarification or add additional context in comments.

5 Comments

I guess, this is one viable solution. But I don't think I'll be able to calculate how the time complexity will grow as the database grows in size. Further the DB is on a server, so multiple processes might be polling it at any given time and hence the query time might not be consistent. Also, multi threading would provide me the capability to do other stuff while the thread is running, won't it?
If you know the DB grows in size and you exec the same query every few secs then you are better with optimizing it with an index and keep constant times as to hide that issue with concurrency. In additioin 10 secs are quite a huge time in our business so that should be more than enough for a query to run. Yes it allows you to do other stuff, but what should happen if the stuff is still not finished after 10 secs - spawn again a new thread, and then again until the machine is out of resources? Of course you can use multiple threads but I would do so only if necessary as it increases complexity
So, is there a way I can execute other tasks which do not deal with the DB in the background if I follow your solution (i.e without threading)?
No, based on your question "I want to make a program that polls a SQL database ..." I thought your program's job is to poll the DB and execute functions based on results of the query and for that I would not use threads as it is not necessary. But if that polling is embedded into another program or your "new" program should do other stuff as well then go with threading.timer. It is as always in IT - "it depends" :)
Aah, thanks. I need to do both actually, some functions will be executed based on the results of query and others won't interact with DB at all. I'll go with threading.timer then. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.