I'm trying to create a program that will initiate a few other functions at a certain second tonight, for new years, but I can't find any answers that work in Python 3. Does anyone have an answer to this? It happens to be 11:58:43 that needs to be executed on.
You can use the datetime module in python to get the present date and time in any required format. Then you can easily implement what you are looking for provided , your python program keeps running checking for this time to be reached.
For this checking loop , it will check time for each second if you put a delay in your checking function as 1 second .
import time
time.sleep(1) # sleeps for 1 second.
I suggest a better method for your purpose .
On windows :- Use the inbuilt task scheduler to run your python program.
On Linux :- Take a look at 'cron' jobs to do any tasks at any specified time pattern . Its very easy to implement.
Thanks to the help of our above friend Natesh bhat, (who will keep his correct answer) I made this script:
import time
time.strftime("%H:%M:%S")
currenttime = str(time.strftime("%H:%M:%S"))
print(currenttime)
#Actual Start Time
timetogo = "23:58:43"
#Time for testing (uncomment)
#timetogo = "18:00:00"
while True:
currenttime = str(time.strftime("%H:%M:%S"))
if currenttime == timetogo:
your_function_here()
Feel free to use it for yourself.
Related
I have a program that will be running 24/7 (assuming no server crashes). The program waits for users of a chat channel to issue commands, then it does several things based on the command issued and user permissions. This program will use and update data from Tuesday 10AM till next Tuesday 9AM, at which point, at 9AM, it will need to wipe the data and start over for the next week cycle.
I'm having trouble finding a way to implement the weekly reset though. I could restart the program every week, but was wondering if there is a way to run a background process that executes a function at a set interval (one week in this case). I was thinking of using a thread to keep track of time, and when a week has passed, have the thread execute the data wipe function. But wouldn't this be an unnecessarily expensive operation? Having a thread running at all times keeping track of passing time seems like a brute force solution.
I would greatly appreciate some pointers on how to go about doing this.
from datetime import datetime
next_wipe = get_next_wipe_datetime() # typically Tue. 9am
...
cmd = read_chat_channel()
if datetime.now() > next_wipe:
next_wipe = get_next_wipe_datetime()
do_wipe()
process(cmd)
I use time.sleep for lots of reasons
But how do I use a time.sleep on only 1 variable?
Is there a:
import time
time.sleep(j(10)) # <- Focus Here
Without me knowing
Do I have to use another command?
Or is it not available on python at all?
time.sleep will have python pause for the amount of time that you say. It will pause the entire program, so using time.sleep won't work on one variable. I'm assuming you want to maybe stop working on one variable but continuing working with another? Since python goes line by line through your code you would just stop writing code that effects the variable. Then once you want to start working on it again you can start writing code that impacts it again.
Using: Python 3.7 on Windows 10
I am making an algorithm that solves mazes. I want to be able to start a timer script when I start the algorithm, and stop when I change a variable, say 'stop'.
The structure of my program is as follows:
import dependencies
algorithm 1
algorithm 2
algorithm 3
algorithm 4
solution
results
On the results screen, the times for each algorithm would be displayed.
Could anyone please point me in the right direction to creating such a timing script? Thx!
P.S. If you wish for more details about the circuit, feel free to ask questions.
You could use datetime.now to get the time delta:
from datetime import datetime
start = datetime.now()
your_algorithm()
delta = datetime.now() - start
Considering that your algorithm interrupts the execution when the stop flag is raised.
I am writing a program for school, and I have come across a problem. It is a webcrawler, and sometimes it can get stuck on a url for over 24 hours. I was wondering if there was a way to continue the while loop and go to the next url if it takes over a set amount of time. Thanks
If you are using urllib in Python 3 You can use the timeout argument in the urllib.request.urlopen(url, timeout=<t in secs>). That parameters is used for all blocking operation used internally by urllib.
But if you are using a different library, consult the doc or mention in the question.
While recording activities in an application through squish in python, I want some wait time in between consecutive activities.
Which function should I use?
You can use the snooze function to suspend test execution for a certain time.
In general however, fixed-time delays are fragile and depend a lot on the system on which the test is executed (and the load of the system). A better approach might be to use the waitFor function to wait for some condition.
For instance, this code acquires a reference to a QPushButton object with the text OK and then suspends test execution until the buttom becomes disabled:
button = waitForObject("{type='QPushButton' text='OK'}")
waitFor(lambda: not button.enabled)
You can use sleep function. For example to put the script sleep for 2 seconds . (Eg:- sleep(2)). Dont forget to import datatime libraries . (Eg:- import time)
# going to sleep for 2 seconds
snooze(2)