How To SCHEDULE Functions & Tasks In Python (FULL GUIDE)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how's it going everyone in today's video we're going to be looking at how we can schedule tasks in Python using one of my favorite packages and that is the schedule package so let's get started immediately by opening up the terminal and typing in PIP install schedule and with this package it just makes scheduling anything you want to schedule very easy which means if you want to post a tweet every 10 seconds or every two weeks at a certain given time this package just makes it super easy so after having installed that I do want to mention that I've created a helper module and all this helper module does is get the current time and returns it in this format I just want to have that so that I can import it into my main file and use it as I please and that's where you're going to see import helper that's my personal helper function next I'm going to import the schedule and I will also import time now to perform a task we need to create a task of course so let's create this task we're just going to call it task and this task is going to print doing task so again this can be posting a tweet or something on social media it can be sending an email it can be running some sort of task on your computer this can be anything you want and we're just going to use the helper dot get time so we can see when this was being executed now I'm going to create a few examples of how you can get started with using the schedule package so first let's type in schedule and we're going to use the first method which is every and here you can put an interval I'm going to say five and I'm going to pick seconds for this one so every five seconds we're going to do the following task and it's as simple as that to create a task to execute this task we need to type in while true and we need to schedule and run pending and this is required to actually run the tasks that have fallen into that five second time frame because just because five seconds has elapsed doesn't mean that python knows that it should run it it needs to check it continuously to understand that there is something to run so we need to use run pending to run everything that is now available to be run and we're going to give this a Time dot sleep of one second otherwise this will Loop extremely fast for no reason but we can actually run this right now and you'll see that every five seconds it's going to perform this task in the console so doing task at 90052 and the next time it's going to be at 57. so it's doing it every five seconds now the beauty of this package is that it is extremely readable so instead of seconds we can also decide to do it in minutes or we can do it in hours or we can do it in days and finally we can also do it in weeks and it was that simple to create a schedule as you can see now it's going to do it every five minutes every five hours every five days and every five weeks and these are five tasks that we've created which means if these are different tasks they will run at different times now let's say you want to be more specific with the time that you want to run a task cat well you can type in schedule every and instead of adding the time there you can actually type in minute for example and we're going to say at and here we're going to add the second that we want this to run out so on every minute at the 15th the second Mark we want to run the following task do and we'll insert our task so it's going to run that each time the time triggers at the 15 second Mark which means it can be 10 15 11 15 12 15. every time it reaches the 15 on the clock on the seconds portion it's going to trigger this task and the same thing works with the hour so this will trigger every time it hits 15 on the hour and it doesn't have to be every single hour you can also say every 10th hour trigger this task at the following minute so every 10 hours it's going to check hey is the time 10 15 or is it 11 15 12 15 whatever time it is 10 hours later it's going to trigger at the 15 minute mark I'm going to remove all of this because you can also do it with the day you can say trigger this every day at the given time and here we can say trigger it at 15 15. so every day it's going to trigger it at this given time and we do need to always provide the do task if you wanted to actually do something of course so every day at 15 15 it's going to perform this task and you can also be as specific as you want you can do it with these seconds as well you can say at 15 15 40 do this task if it's really important that it performs the task at that given time frame and another great feature of this is that you can say do this every Monday or every Tuesday Thursday whatever day of the week you want and it's going to work as well you can say do it every Monday and it will perform this task or you can also do every Monday at 1515 and it will perform that task every Monday at this time frame so you can get very specific with this package and that is just wonderful how easy it is to create a schedule without having to worry about time dates and so on I also want to show you that this package has a decorator for your jobs your tasks your functions just to make it a bit more easy for you so we can type in from schedule we're going to import repeats and every so we're going to import those two just to make it look a bit cleaner so we can use this decorator now on the function or the task that you want to run and here you just type in every let's say 10 seconds and it's going to run this function every 10 seconds now so we don't need the schedule down here anymore we can just run it as is and if we run it it's going to run in the console every 10 seconds but at this point I'm sure you're wondering well it's been easy to start tasks without any arguments but what if you actually want to pass in an argument to your functions when you are scheduling them well let's stop the program and try to do that so we can remove this part right here for now and we will just create another schedule so schedule every five seconds and what we want to do is the task so do and we'll pass in the task right now the task doesn't take any arguments so let's create some arguments so ARG one and arg2 for example and we're going to we're going to edit this function just slightly so we're going to say args are equal to and I want this to be a formatted string so args are going to be equal to and we're going to add ARG one and arg2 so this will create a tuple because we have a comment there but it's going to use those arguments in the task just to show you that we can use them and to use them is actually really simple everything that comes after The Comma of the task will be passed in as an argument so here you can say 10 as a string and you can say Luigi and 10 doesn't have to be a string actually can also be an integer or anything you want to pass in as an argument now if we run this you'll see that every five seconds it's going to run this task with the given arguments inside so doing task arguments is equal to 10 and Luigi at this time over here and five seconds later it's going to do it again and that's super cool but what about with The Decorator how would you do that with the decorator and it's really the same thing just type in repeat and you need to repeat how many times so you say every five seconds and then you just pass in the arguments you say five and Luigi and it will work exactly the same way now we can run this program and every five seconds it's going to perform this task with the given arguments that you've put inside repeat what's really cool about the repeat decorator is that you can add multiple of them so you can duplicate this and you can say that every six seconds you want to print the value of zero and Mario so now if we run this and wait five seconds Luigi will be printed and if we wait six seconds Mario will be printed as you can see in the console here Luigi is being printed every five seconds but Mario's being printed every 6 seconds which is really cool because you can add many variants to your repeat decorator but let's simplify all of this again let's remove the repeat keyword and remove the arguments from our function just to keep it as simple as possible because what I need to show you next is how you can cancel a job or a task so here we're going to type in job is going to equal schedule dot every and we're going to say our at the 30 minute mark and I don't think you need the zeros actually that would be absolutely wrong and we're going to do the task so to cancel a job at any point you just need to type in schedule dot cancel job and pass in the job and just by doing that it's going to make sure that this will never run again and you can actually confirm that by printing schedule dot all jobs or get all jobs I think yeah get jobs but let's run that and you'll see that when you try to get the jobs there will be nothing in there because we've canceled it ahead of time if we do not cancel that and we run it we will have the job inside our list when it comes to getting all the jobs but now comes another question what if we want to run our job or our task only one time well there is some special syntax for that and to do that you would have to actually return a special type and here we're going to return schedule dot cancel job inside the task that you want to run only one time because once this triggers this will tell the job that okay this was the only time you should have used it now cancel it so we don't have to run it again so now we can type in schedule dot every five seconds do the task and I'm also going to start adding this print statement at the bottom and the print statement is just going to say jobs and we're just going to get back the length of schedule Dot getjobs so this will help us understand how many jobs we have that are actually active but let's run this now and what you'll notice in the console is that run pending is going to be triggered every second but until the job is actually ready to be run it's not going to run but once it is ready to run it's going to run that task and then it's going to remove it from the active jobs which means we'll have zero jobs left and this will run forever because it is only going to run what's pending but we have no jobs to be run so it's not going to run anything after it has been canceled now that's pretty cool but what if we have a lot of jobs at a certain point in our program we just want to cancel them all well there is another thing we can do that and that is to call schedule dot clear so now it's going to run the pending so every five seconds it's going to print all of these tasks but at the end it is going to clear all of them so when we run this it's going to start with having four jobs in total but as soon as we clear it it's going to have zero jobs which means if we run this you'll see the first second will have four jobs the second second will have zero because on this first Loop we cleared them immediately another really cool feature with this schedule package is that you can add tags to your schedules which means you can organize them and you can actually filter them and this is actually very important to know because pretend you have some schedules you can say schedule every minutes and you want to do the following and of course it will be our Infamous task but this time we can add a tag such as work and we can add multiple tags in case you want to associate them with different options so now we have two tags here we have work and one and you can add more tags or less tags but that's solely up to you so we have this one here and we're going to duplicate it four times we're going to say every hour every day and every week and here we're going to say fun and work and fun but we're going to change the last two so they all have different tags and now we can actually filter those in case we want to run certain tasks or we want to cancel certain tasks we can now refer to the tag to actually fetch them run them or delete them so for example if we want to get all the fun tags with type in schedule Dot getjobs and we'll say by Fun and alternatively we would do the same thing for the work so he will type in work and we will get all of the jobs associated with this work variable just to prove all of that we can print fun and we can print work and I'm just going to comment out this for now because we don't need it and if we run this you'll see that we'll get two lists back with two jobs in each one of these lists but we do not need this at the moment and let's uncomment this and I'm also going to print the jobs above time.sleep because here I want to show you that we can schedule and clear jobs by a given tag such as fun because we do not want the fun jobs now if we run this it will start with four jobs and then it will go down to two jobs because we cleared all the jobs that had the tag of fun so that's really cool that you can filter your jobs just by adding some tags but up next I want to show you how you can run your jobs at random time intervals so we don't need these down here just to make it simple again and in between task and while true we're going to add schedule dot every one two ten seconds we're going to do the following task and again you can see how easy it was to create a function or a schedule with this package it's plain English and that's why I love this package so much and I really recommend you use it if you want to create schedules because it's just easy to read and easy to use and if we run this every 1 to 10 seconds it's going to do the following task and you'll see it in the console that every one to ten seconds it's going to perform it it can be 10 seconds it can be 5 Seconds it can be seven seconds but it's going to fall in that range but there's something else that's very important that you should know how to do and that's how to schedule a job until a certain time so we're going to say every 10 seconds Dot until and here you can give it a certain time so the easy way to do it is to give it an hour such as 1910 and we always need to call do task so this will run every second every 10 seconds until this given time and then it will stop which is quite cool but it's not very specific if you want to also mention a date so this actually works with daytime as well so if you want to you can just paste in your date and time like this I mean I need to import it of course but you can paste in your date time and it's going to run this until 2024 and that will finish on the 12th month on the 31st day at the hour 10 20 minutes and 15 seconds so you can get really specific on how far you want this to go until and after that it's not going to run anymore we now officially have an end time for our task to run now we're getting near the end of how to use this package I only have two more examples to show you and these are quite important so first I'm going to paste in this over here where you can see we're scheduling this task every Monday at 10 and every Tuesday at 15 but maybe something urgent has come up so we're going to call schedule run all and this is going to run all of the tasks as soon as we call this method and there's also an optional argument that you can pass in and that is the delay in seconds so if you pass in a delay in seconds of 10 it's going to run the first function and then it's going to wait 10 seconds before running the second in case you don't want every job to run at the same time so let's add a one second delay here because if we now run this in the console it's going to use the task immediately and you'll see that it used both of them with a one second delay in between moving on to the last thing I want to show you about schedule and that is how to run threaded tasks because right now we run into the issue that if we run a task that is blocking who knows maybe you're making an API request there's something that's blocking your program it's not going to be able to perform another task until this task is done so let's actually change that to timesleep.5 seconds and to prove this we're going to type in schedule not every one seconds do the following which is the task and we're going to get rid of run all but check this out as soon as we run the program we will get the first output but it's not running them every one seconds it's only run them every five seconds because we have the time dot sleep that blocks the execution of our lines of code because this is a blocking task and that's not really good because maybe you want some tasks to be run concurrently so the solution would be to import threading and we need to create another function that starts a thread so here we'll type in Def start thread and it's going to take a function and it's going to take a job 1 which will be threading dot thread because we need to start a thread and here we'll add the target which will be the function itself and we'll type in job1 dot start so now we have this function that starts a thread so what we have to do instead now is start the thread and pass in the task or the job so with this simple fix we can now rerun the program and it's going to start the task every second as we've planned even if they are blocking and have a five second sleep it's still able to start these tasks without blocking our program but anyways that was actually a lot of explaining I've been talking for 30 minutes straight now of course I had to edit it down because it is a lot of content that I have been messing up on so if you did enjoy this video please do leave a like it really helps me with understanding whether I should make more videos such as this one or whether I should try something else and if you do have any suggestions on videos that I should make or something that you found interesting on in the python documentation please do share it in the comment section down below I would love to make a video about it if it sounds interesting but as always with all that being said thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 36,930
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: FCPBG6NqMmQ
Channel Id: undefined
Length: 19min 48sec (1188 seconds)
Published: Sat Feb 11 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.