go back  50 - Use Celery to Offload an Expensive Task




This challenge write-up first appeared on PyBites.

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:

  • Do you want to discuss this challenge and share your Pythonic journey with other passionate Pythonistas? Confirm your email on our platform then request access to our Slack via settings.

  • PyBites is here to challenge you because becoming a better Pythonista requires practice, a lot of it. For any feedback, issues or ideas use GH Issues, tweet us or ping us on our Slack.

  • And of course Happy Easter!


>>> from pybites import Bob, Julian

Keep Calm and Code in Python!