Code Challenge 50 - Use Celery to Offload an Expensive Task

PyBites, Mon 02 April 2018, Challenge

async, broker, Celery, Easter, Flask

It's not that I'm so smart, it's just that I stay with problems longer. - A. Einstein

Hi Pythonistas, back-to-back with our Planet Python challenge 49 here is our special Easter Challenge #50 where you will use Celery to offload a simplified Easter ecard mailer app.

The Challenge

Go to Challenge 50 on our platform and pull/update the Community branch. cd into the 50 directories, create your virtual env and pip install the requirements (Flask and Celery).

The app let's your enter a banner URL, a comma separated list of emails and optional message:

simple flask app for celery

simple flask app for celery

The _emails_users simulates some processing time by sleeping 1 second - in real life this could be an intentional short pause so as to not hit a server or API too often. For the end users though, the page just appears slow and could result in them navigating away while the emailing is still taking place! Time to add some asynchronous processing so the user can keep navigating the site!

Can you offload the emailing to a Celery task so the user does not have to wait for it to end or to prevent an impatient user from navigating away? Change the Flask app as you want, it's just some bootstrap code to get started ... You could also add an option to the form to send the card at a later date ...

Set up a message broker of your choice and start playing with Celery. For a Flask + Celery basic example check here.

Bonus

Bonus points if you can display progress of the emailing on the page. Secondly try to deploy it to a cloud service provider and make the emailing work. You could try Heroku + Sendgrid for example.

Credit

PR your work on our platform and get our special PyBites Easter Badge added to your dashboard.

We are moving the review posts to a featured view on our platform (audience > 1800 users and growing), PR and standby ...

PyBites Community

A few more things before we take off:


>>> from pybites import Bob, Julian

Keep Calm and Code in Python!

PyBites Python Tips

Do you want to get 250+ concise and applicable Python tips in an ebook that will cost you less than 10 bucks (future updates included), check it out here.

Get our Python Tips Book

"The discussions are succinct yet thorough enough to give you a solid grasp of the particular problem. I just wish I would have had this book when I started learning Python." - Daniel H

"Bob and Julian are the masters at aggregating these small snippets of code that can really make certain aspects of coding easier." - Jesse B

"This is now my favourite first Python go-to reference." - Anthony L

"Do you ever go on one of those cooking websites for a recipe and have to scroll for what feels like an eternity to get to the ingredients and the 4 steps the recipe actually takes? This is the opposite of that." - Sergio S

Get the book