Sending Emails in Django With Celery

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in today's video I'm going to walk you through the process of setting up celery with Django so you can send emails using celery so celery is a distributor task queue that allows you to send tasks to this queue that would be executed by celery outside of the regular context of your app so the use case for this is usually when you have some long-running process that needs to be called from one of your views so someone goes to a page in your app and there's some long-running process that gets triggered by them going to that page instead of having them wait for that process to finish running what you do is you sit it over to celery and then you continue along with a process in your view in return whatever information to the user and then celery will execute that separately and then return the results elsewhere in your app and you can do whatever you want with it so in the case of sending emails emails don't necessarily need to return anything they just need to be sent so instead of having a user wait for the email to be sent you let celery handle sending the email so this requires a couple of parts you need to have the jangle app of course but in addition to that you need to have a celery server running and you also need to have some kind of task broker running so that's what we're gonna set up in this video so we're gonna use Heroku for the broker we're going to use Django for the app of course and then we'll install celery to run the celery server on your local machine so to get started I already have jingle installed I'm going to start a project so we'll use Django admin start project and we'll call this Django email celery and we'll start an app we'll just call this example doesn't need to be anything special okay so I have that I'll open up the directory here example and jingle email celery I'll go to settings and I'll add the app so example here and then while I'm at it I'll install celery so while that installs I'll go to the documentation for celery and there are some in that we need in our project so they have the code written already so you can just bring it into your project so it's not like you're installing a Django celery library and said you just bring this in you create a folder called celery or a file called celery so I'll go back to my project and in here I'll create celery dot pie and paste this and you just need to update the project here and here with the name of your actual project so Ango email celery and I'll put that here so two places so what's happening here is this just allows a celery to interact with our jingle app so the way that it works is it's going to be running on a separate server but the tasks that it can run will come from our jingle app so this allows celery to look into our jingle app and see what we have and use it to execute the tasks that are defined and I'll define a task later so basically this sets the location of these settings and this here loads in the settings but this is the important part it creates a new instance of celery calls an app and then also we have this app autodiscover tasks which will discover all the tasks that we have set up in our project using a decorator that I'll show you how to use once we get to it so in addition to this file I need to update my dunder knit for the project directory so they have some code here so this dunder init here and you just want to bring this in so I'll go here and paste it so now that I have those two things done I can start using celery so what I want to do is in my example I'm going to create a view that we can use and that's going to trigger everything for celery so I'll just call this index and our return and HTTP response same done because not really important what it returns what's important is that our task runs so what I'm going to do is I'm going to import this HTTP response so from django HTTP import that HTTP response just like that and then what I can do is I can set up the URLs so I'll goes to the URLs for the project first and I'll set a path to be just blank now include examples dot URLs which I haven't created yet and then since the code here is going to be similar I'll copy that and I'll create a URLs file in the app example I'll remove this path our move include and this is going to be from views so from dot use or if from dot import infuse that's what I want and then views dot index and we'll give it a name of index as well even though I don't think I need to use the name for anything so now that I have that what I can do is I can try running the apps and make sure that it actually works so we'll run the server and then we'll go to the app and it is port 8000 and we see done over here so we know the app works so now let's start working with the celery task so before we can work with the celery task we need to set up something called a task broker and the task broker is going to hold the list of tasks that are pending so the way that it works is when we call a task in our jingle app it's going to send that task over to the celery server then the celery server is going to send that task over to the broker or the queue and it's going to wait there until the celery server comes back and picks it up again so this allows us to delay running tasks but in our particular instance we're gonna run the task as soon as possible but if we wanted to we can put a task on the queue and leave it there for like a day and then run it a day later but the basic process is the celery will send the task to the queue and then read the task back from the queue so there won't be much time here but that's just how it works so to do this I'm using Heroku and I'm going to set up a new app so create a new app and we'll call this Django email celery example and this should be dashes instead of underscores so let me just replace those with understorm replace those with dashes I should say and it's available so we'll create the app so normally after you create an app on Heroku you would set it up to actually load in the code from your app but the reason why I'm creating the app on Heroku is because I want to use something for the broker so what I can do is I can go to add-ons here and then I can search for an add-on and there are a few that you can use for this you can use a database which is an ideal you can use Redis you can use RabbitMQ we'll use Redis because it's really easy to use so this is where the task queue is going to be stored and I'll hit provision so this is going to go ahead and set up and while that sets up I'll go and I'll work on the options for the yap are these settings for the app so we need to specify where the broker is and then we need to specify some other things so if we go to settings up PI and just go down to the bottom we'll need a celery broker URL just like that and the URL is going to come from Redis so I'll put that in once Redis is done starting up we'll also need a celery accept content along with a celery task serializer so this is basically the format that the tasks are stored in so it's just Jason for both the first four accept is a list and a second is just a string and let's see if B Reta server is ready yes it is so we just click on that and it's gonna open up and this usually takes a moment but what we're gonna get out of here is a URI for it and we're gonna put that URI back in here for celery broker URL so if we go back we're just waiting for this so it's ready so view credentials and then we see the URI down here we're just going to copy this and paste it into our settings for the celery broker URL okay so now that that's ready what we need to do next is we need to start up our server so actually you know before we started the server let's create a task so if we go over to our example app here what we want to do is we want to create a new file and we'll call this task dot pi and in here we're going to import from celery import a function called share task and this function is going to be used as a decorator so share task like this and underneath it we can create a function so this first function is going to be something that just sleeps so I'll call this sleepy and how imports sleep from time so from time imports sleep and let's call this duration and basically what I want to do is call this sleep function with the duration that's passed in and then return none because it doesn't need to do anything after that so the reason why I'm doing this is to demonstrate how a celery is asynchronous so if I have a sleep of ten seconds normally if I call this function inside of my view then the user who goes to my page has to wait 10 seconds for the view to return but if I use celery it should be instant because it sends the sleep off to celery to be executed and then the user can I get the results of the page immediately so I'll take the sleepy and I'll import it so let me close the url's files and I'll go to views so from dot task import sleepy and I'll just call sleepy with 10 seconds here as a delay so before I even start up celery I just want to demonstrate that this is going to take 10 seconds so I'll go to my app again now run it and it's hard to see but it's loading and this process should take about 10 seconds because I have a sleep duration of 10 seconds so it just finished so that took about 10 seconds so what I want to do now is I want to send this off the celery so this will return immediately so to make this more clear our first task is done let me just use HTML so we can see it but to use this with celery is pretty easy we just to set up the server so by installing celery all we have to do is run a task so or we have to run a command I'll open up my console where is it here and let's see so this is tango email celery and then inside of here I'll start up the shell and then what I can do is I can CD into my project directory so same name and then I can see all my files here and what I want to do is I want to start the server so the command is going to be celery - a and then the name of your project so tango email celery and then we need to have worker here - L and then we can have info to give us more info about what's going on so run that and we see no such transport let's see what the issue is so let's make sure the server has been saved with the new settings oh there's a giant space here with a broker URL so let's try that again so I will start the server server works for Django we'll come back here to celery and we'll try that command again and this time it says missing Redis library so that's good so we just need to install Redis so pip in install Redis and this just allows us to connect to a read a server and not necessarily run a read a server on our machine because we have it on Heroku already okay so now that's installed let's try this again and this is what you should see when it works so we can see the two tasks that our app has we have debug task which was given to us in the code that we copied and we have this sleepy task so this part is working but before we can use it let's demonstrate how to actually activate it so the only thing we need to do for sleep or sleepy here when we call it in views is that dot delay here so we still have the parentheses it's just that delay will become the function instead of sleepy so sleepy dot delay so this delay is added to the function through the share task decorator so if we save this and I just ran the wrong command we run the server and then we go to a page one it should return a lot faster and two we'll see some things happening on the console and here and Redis so let's try it so we see it did not take ten seconds and then if we go here we see our receive tasks example dot task sleepy and if we look at Redis momentarily we should see maybe if i refresh the page we should see some activity down at the bottom because we're now using the server in some way and that's what we see like there's not much going on because I've only done one call but we can actually see stuff has happened on a server so with the sleepy we can't actually see anything happening because all it does is it sleeps somewhere on the celery server for ten seconds which isn't very useful but you should see that it returns a lot faster than it did when we didn't have the delay so now let's have something a little more interesting let's send an email and that way we can actually see something happening so to send an email the first we need to set up the email settings so if we go to Settings app I and then just go down to the bottom you need to put in email information so this always depends on the email host that you're using so the settings may not match mine exactly so you have to look at the documentation for your email host and to figure out exactly what they need so I'm gonna use the information for mine but for yours it's probably different so we're gonna have a user email host password and then email use TLS and email use SSL so for mine SSL will be true TLS would be false a password by copy is this support at pretty brunette calm is the email that I'm going to use the port is 465 and then my host is fast mail so SMTP dot fast mail and I'm gonna change my password after this video so don't try to send something from my email but um this is all the information you need to set up the email and when sending emails you may want to use debug false for this because there are some issues with debug mode especially with celery so just put your localhost as the allowed host and set debug to false and then you can go back to tasks and in here we're going to create a task for sending an email so we'll have the share task again and then this time we'll call this function sin email task we'll call it and we'll just write all the code in here to send the email so from Django Cornell we can import the send mail function which is going to use that configuration that we just set up to send an email somewhere and what I want to do is I want to send a mail and I'll say um celery tasks worked and then I can say this is proof the tasks worked so this is the body of the email then I need to have the from address which is support at pretty calm and then I need to have a recipient so I have a temporary email service set up here so I'll just copy this email I'll put it here and then I think that should be it so this will return none so let's go over to here and what I'll do is I'll put another sleep in here so I'll put a sleep for 10 seconds just so we can see it working again and then I'll go to views and instead of calling Sleepy delay I'll call send email and so CIN email so the first time is going to be without celery so email has been since okay so let's try this so I'll go to my app run it and we get a server error so I messed up somewhere let's see and guess I need to turn on debug mode again to see what's going on so debug it's true and let's try this again okay so CIN emails not defined that's a pretty simple err so needs to import that from tasks so I'll set this back to false and it's saying I can't import it so I'll send email tasks that's why I know I was thinking send email to send email tasks here and that should be it so it's still on the development server so debug false I was restart the server okay so for some reason it's not picking up a change from debug so debug false I'll start okay so let's try it now run this so we have the sleep so it should take ten seconds and it says the email has been sent and we get an email from a dating site that was pretty fast that they managed to find this email but we also have the celery tasks worked so we see that it was working so this is proof that the tasks worked so we saw that it took a while for it to work so now if we put the dot delay on here so we go to views and then send email tasks dot delay now it will be handled by celery so let me restart the server and then what I'll do is I'll say email has been sent with celery and this should return a lot faster okay so let's try we run it it immediately returns and let's see if we get another email so just waiting the sleep is still there but the sleep is now running on the celery server itself so it's not running on my Django server so we're just waiting for the sleep to be finished and then the email to be sent and I don't know if it's the service or what's so this one so there was an issue so because we added a new task I needed to restart the server because it only had the sleepy task so let's just restart the server and now we see this in email tasks so now it should be able to work so should be connected now and then if we go back to the app run it again we look at the console we should see receive tasks and email tasks ok so now we wait the 10 seconds and then we should see the email up here in the Inbox and there we see it we see the second email so instead of our app trying to send the email as the user is you know triggering something in the view to process and said it sends it off to celery and it continues the execution so celery is really useful for anything that could possibly take longer than usual so anytime you're connecting to another service like another server for some reason you don't eat the results right away and you can use celery to do this so using something like celery there can be a lot of issues but as you can see in the video we were able to get past them pretty quickly it's just you know a lot of things you have to take into account so that's it for this video I just want to remind you that I have courses on my website available if you are interested in learning more about Django you can try out the Django database essentials course it's free you'll learn how to use different things within Django models so how to perform different queries how to set up relationships and so on so that's it for this video if you liked this video please give me a thumbs up if you have any questions about this video feel free to leave a comment down below and if you haven't subscribed to my channel already please subscribe so thank you for watching and I will talk to you next time you
Info
Channel: Pretty Printed
Views: 57,049
Rating: undefined out of 5
Keywords: celery, django celery, python django celery, django celery email, send emails django with celery, use celery with django, send emails django, python, django, tutorial
Id: b-6mEAr1m-A
Channel Id: undefined
Length: 23min 19sec (1399 seconds)
Published: Sun Sep 08 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.