Timed email reminder in python

…衆ロ難τιáo~ 提交于 2019-12-08 07:33:35

问题


I have written up a python script that allows a user to input a message, his email and the time and they would like the email sent. This is all stored in a mysql database.

However, how do I get the script to execute on the said time and date? will it require a cron job? I mean say at 2:15 on april 20th, the script will search the database for all times of 2:15, and send out those emails. But what about for emails at 2:16?

I am using a shared hosting provided, so cant have a continously running script.

Thanks


回答1:


If you cannot have a continuously running script, something must trigger it, so that would have to rely on your OS internals. In a unix environment a cron job, as you self state, would do the trick.

Set cron to run the script, and make the script wait for a given time and then continue running and sending until the next email is more than this given time away. Then make your script add a new cron job for a new wakeup time.




回答2:


Looks like this django application was made just for people in your situation...

http://code.google.com/p/django-cron/

Also, your design seems a little flawed. If its running at 2:15 you wouldn't want to send out just emails that should be sent at 2:15, but all ones that should have been sent in the past that have not been sent.

Your database should either: A. Delete the entries once they send or B. Have a column defined on your database table to store whether it was sent or not. Then your logic should make use of that column.




回答3:


A cronjob every minute or so would do it. If you're considering this, you might like to mind two things:

1 - How many e-mails are expected to be sent per minute? If it takes you 1 second to send an e-mail and you have 100 e-mails per minute, you won't finish your queue.

2 - What will happen if one job starts before the last one finishes? Be careful not to send e-mails twice. You need either to make sure first process ends (risk: you can drop an e-mail eventually), avoid next process to start (risk: first process hangs whole queue) or make them work in parallel (risk: synchronization problems).

If you take daramarak's suggestion - make you script add a new cron job at end - you have the risk of whole system colapsing if one error occurs.



来源:https://stackoverflow.com/questions/2732407/timed-email-reminder-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!