Automated Processes

23 views
Skip to first unread message

Scott Macri

unread,
Mar 2, 2012, 8:10:43 PM3/2/12
to django...@googlegroups.com
I'm trying to figure out the best way to create an automated in a
python/django web application. My intention is to send an email
message in the future based on the given future date/time. What is
the best approach to do this?

I was thinking of creating a cron job and a python script, but I would
rather have the process be part of my django application so I don't
have to worry about extra stuff.

Another thought was to use a time, but if the timer process stops
before the message is sent, that's it KABAAAAAMMMMM.

Any thoughts on how to handle this situation? Thanks.

Scott

Scott Macri

unread,
Mar 2, 2012, 8:11:30 PM3/2/12
to django...@googlegroups.com
Sorry, that should read "create an automated process".

--
Scott A. Macri
www.ScottMacri.com
(571) 234-1581

Jamie Lawrence

unread,
Mar 2, 2012, 8:20:40 PM3/2/12
to django...@googlegroups.com

On Mar 2, 2012, at 12:10 PM, Scott Macri wrote:

> I'm trying to figure out the best way to create an automated in a
> python/django web application. My intention is to send an email
> message in the future based on the given future date/time. What is
> the best approach to do this?
>
> I was thinking of creating a cron job and a python script, but I would
> rather have the process be part of my django application so I don't
> have to worry about extra stuff.

If you want to have the active code in Django, you could run a cron to call wget or similar to call a specific URL that does whatever. If you choose to do this, make sure it is locked down - perhaps only execute if REMOTE_HOST is 127.0.0.1.

I generally don't recommend this approach, because it really is mixing distinct functions. What happens if you grow to multiple servers? What happens if you're sending enough mail that you split that off to its own cluster?

Note that it should be straightforward to pull in the Django template classes, if you want to use that for generating your messages.

Also, note that this is precisely what cron is for. Attempting to recreate cron inside of a web application is going to end in tears. Keep your cron job and support scripts in a separate directory in your source repository, and add a checklist for setting it up to your documentation (which is also versioned, right?).

My .02 units of your currency,

-j

Russell Keith-Magee

unread,
Mar 3, 2012, 4:48:57 AM3/3/12
to django...@googlegroups.com

On 03/03/2012, at 4:10 AM, Scott Macri wrote:

> I'm trying to figure out the best way to create an automated in a
> python/django web application. My intention is to send an email
> message in the future based on the given future date/time. What is
> the best approach to do this?
>
> I was thinking of creating a cron job and a python script, but I would
> rather have the process be part of my django application so I don't
> have to worry about extra stuff.

...


> Any thoughts on how to handle this situation? Thanks.

Yeah - use a cron job and a Python script :-)

Seriously -- Django is good at what it does, but just because you've got a really good hammer, it doesn't mean every problem is a nail. The task you describe sounds like almost exactly what cron scripts are designed to handle. Rather than trying to bend Django into a shape that will solve your problem, use the right tool for the job.

You might also be able to attack the problem using a task queue like celery [1]. However, celery is more complex to get set up than a cron script. There are lots of benefits that come with that complexity, but if you just want to get something going, you don't have any experience with task queues, and you're not expecting any serious load problems, cron will be the easier option.

It's also worth pointing out that you can still use parts of Django (e.g., your models, the ORM, the mail sending utilities) in a standalone Python script that is called by cron/celery. Django is a just a set of Python libraries. Yes, it's a set of libraries that is usually used to build web sites, and most examples use Django APIs to service HTTP requests, but there's no reason you can't write a standalone Python script that uses Django's APIs to access and manipulate data.

[1] http://celeryproject.org/

Yours,
Russ Magee %-)

Scott Macri

unread,
Mar 3, 2012, 4:53:26 AM3/3/12
to django...@googlegroups.com
Awsome, cron it is! Thanks. :)

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

creecode

unread,
Mar 3, 2012, 3:40:10 PM3/3/12
to django...@googlegroups.com
On Friday, March 2, 2012 8:48:57 PM UTC-8, Russell Keith-Magee wrote:

Seriously -- Django is good at what it does, but just because you've got a really good hammer, it doesn't mean every problem is a nail. The task you describe sounds like almost exactly what cron scripts are designed to handle. Rather than trying to bend Django into a shape that will solve your problem, use the right tool for the job.

You might also be able to attack the problem using a task queue like celery [1]. However, celery is more complex to get set up than a cron script. There are lots of benefits that come with that complexity, but if you just want to get something going, you don't have any experience with task queues, and you're not expecting any serious load problems, cron will be the easier option.

It's also worth pointing out that you can still use parts of Django (e.g., your models, the ORM, the mail sending utilities) in a standalone Python script that is called by cron/celery. Django is a just a set of Python libraries. Yes, it's a set of libraries that is usually used to build web sites, and most examples use Django APIs to service HTTP requests, but there's no reason you can't write a standalone Python script that uses Django's APIs to access and manipulate data.

Scott as Russ points out you can still use parts of Django.  I've had good success with cron and custom django-admin commands.  There is some overhead to write the commands but the upside is that they give you a defined way to approach the problem within the realm of Django.  Also since they are django-admin commands you can run them as you would any other django-admin command manually which can be convenient at times.

Toodle-loooooooooooo..............
creecode
Reply all
Reply to author
Forward
0 new messages