I use a python script (on a linux web server) to redirect a user on request. The redirection is based on a database (a python dictionary) and the database itself is builded from a remote CSV file.
For now, I have to manually update the database but the CSV file can change at any time.
I'm looking for a way to update the database after each user request (after 10 sec). In this way, the database is always开发者_如何学Python up to date and the user do not suffer from the update.
I'm trying with the shed module but it doesn't work.
import sched, time
s = sched.scheduler(time.time, time.sleep)
s.enter(0, 1, app.redirect, ())
s.enter(10, 1, app.data_base_update, ())
s.run()
The goal is to keep the url redirection fast for the user and delay an update later. Is there a good solution to do it with a unique script file?
You would be better served by updating a COPY in the background, and instantly switching them and making the updated copy into the live copy. Thus there would be no wait for the user, and you could do so at any time. You are probably best not doing so 10sec after each user request (imagine a flood of requests... it will bring your server to its knees). You can schedule a cron script or other automated task to do so, every minute or half hour etc.; depending on the size of the task you can also limit the CPU utilization.
Note that your solution still doesn't ensure the database is always up to date, since you are working with remote data. But that is unfortunately the price of working with remote data. Make sure not to hammer the remote servers if they do not belong to you. =)
精彩评论