In this blog post, I will run through 3 different ways to execute a delayed Python loop. In these examples, we will aim to run the loop once every minute.
To show the delay, we will print out the current datetime using the datetime module.
1 – Sleep
The sleep function from Python’s time module pauses the Python execution by the number of seconds inputted. The example below pauses the script for 60 seconds.
The above script has a 60 second delay between the end of a loop and the start of the next. However, what if we wanted to execute the next loop 60 seconds after the start of the previous loop.
In other words, how do we start the loop at the same time every minute.
2 – IF Statement
To do this we will switch to a WHILE loop and use an IF statement to see how many seconds have passed in the current minute. A WHILE loop is used because it will execute until i = 10, until the datetime has been printed 10 times. The IF statement checks the time to see when the print statement should be executed.
We will print the datetime every time the minutes changes. The Sleep function will continue to be used as the loop would execute multiple times in one second if we didn’t.
date_time[-2:] – is the last two characters of our datetime variable which is the number of seconds that have passed this minute.
3 – Task Scheduler
The previous two options are good for executing a loop a few times, ten in our case. If we wanted to execute a python script continuously without expiring, we could use the above examples with an infinite appending loop.
However, if one loop errors the script will stop. Therefore, we want to execute the entire Python script once a minute using an external trigger. This is where we can use Task Scheduler.
Task Scheduler can execute a python script from source but it is often easier to use a batch file. The batch file includes the location of the python application (python.exe) and the location of the python script (.py). For more detail on using Task Scheduler and batch files to run Python scripts, please see the following datatofish post – https://datatofish.com/python-script-windows-scheduler
Our batch file is:
To demonstrate Task Scheduler, I’m going to run the following Python code every minute.
This code uses Pandas to produce a blank CSV file, but the name of CSV file is the datetime the script was run.
These following screenshots show the triggers and actions used.
This produced the following CSV files, we can see that the files takes 1 – 4 seconds to create.
In summary we have seen three different ways to delay a Python loop, two using loops inside Python and one using Task Scheduler. All can be used depending on what kind of delay is best.
There is a very serious limitation in the behaviour of calculation groups when using the SELECTEDMEASURE or ISSELECTEDMEASURE functions, and we recommend not using them. Why? If a user […] Continue Reading…
If you’ve been doing any development work in ADF this week you might have noticed that “Connections” has moved. But where has it gone?
The old location
When you click onto […] Continue Reading…
In this blog post we’ll take a quick look at using ConcatenateX function to view a concatenated string of dates where the max daily sales occurred for a given […] Continue Reading…
If you’ve not come across regular expressions (RegEx) before then you’re missing out, they’re an incredibly powerful tool for matching, extracting or validating patterns of text.
I had a use […] Continue Reading…
A common request that is raised by clients is how to sort a table in Power BI by multiple columns, in the same way you can in Excel.For a […] Continue Reading…
When querying data from Azure Databricks (spark) into Power BI you may encounter an error: “ODBC:ERROR [HY000] [Microsoft][Hardy] (100) The host and port specified for the connection do not […] Continue Reading…
I recently came across the following error while deploying a tabular cube using the Analysis Services Deployment Wizard:
My updates for this deployment included removing a table from the cube. […] Continue Reading…
Azure Data Factory (ADF) Parent Activities don’t trigger ON ERROR OUTPUT when Child Activities Error… What happens?
So that title is a big question that a client of ours came across that required some additional investigation… What does happen and how do we get around it? […] Continue Reading…
By Nick Edwards
In this blog post we’ll take a quick look at creating a self-generating calendar table using DAX.
Dates are important if we want to perform time intelligence reporting […] Continue Reading…