Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
The community reviewed whether to reopen this question last month and left it closed:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
Improve this question
I want to run a script, or at least the block of code in it every 12 hours. How bad/ how much resources will I be wasting by using:
while True:
My Code
time.sleep(43200)
Is there an easier and more efficient way to accomplish this?
I'd recommend using apscheduler if you need to run the code once in an hour (or more):
from apscheduler.schedulers.blocking import BlockingScheduler
def main():
Do something
scheduler = BlockingScheduler()
scheduler.add_job(main, "interval", hours=12)
scheduler.start()
apscheduler provides more controlled and consistent timing of when the operation will be executed. For example, if you want to run something every 12 hours but the processing takes 11 hours, then a sleep based approach would end up executing every 23 hours (11 hours running + 12 hours sleeping).
this timing is not accurate as it only count time when cpu is sheduled on this process
at least you can check target time is arrived every several second
and this is not a good solution as your process is less reliable than system cron. your process may hang due to unknown bugs and on system high cpu/mem utilization
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 months ago.
Improve this question
Good day.
I have a question about the correct way of implemting code that needs to run every 5 minutes.
Is it better to:
A - Inside the code have a timeloop that starts after 5 minutes, and
executes.
B - Have a script that runs every 5 minutes and executes your
application.
C - Other?
BG: This will be running on a windows server 2022, to send mail every 5 minutes if certain condations where met.
Thank you.
B.) The script is named Windows Task Scheduler and comes with permission management etc.. A Windows server admin can tell you about it.
Why?
Your app might have memory leaks (well, Python not so much) and it runs more stable when it's restarted every time.
An app that sleeps still uses memory, which may be swapped to disk and read back when it awakes. If the app terminates, the memory will be freed and not be swapped to disk.
Your app may crash and no longer do what you expect to be done at every interval
The user may (accidentally?) terminate your app with the same effect
Why not / when not?
If the time to initialize the app (e.g. reading data from database or disk) takes a long time (especially longer than the sleep time).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
I want to create a code that charges 'money' for every second that my program is ran. For example, if I am charging 1 cent for every second that the program is ran, and the program is ran for 10 seconds, I want a statement to be printed like "You have ran the program for 10 seconds, and will be charged 10 cents" at the end of my program.
I don't even know where to start on this, but so far, I've created a code that shows me how long my program has been ran for. The code looks like this:
from datetime import datetime
start_time = datetime.now()
#my code
end_time = datetime.now()
print("Duration:", end_time-start_time)
How can I go from here?
You're mostly there; just get the total_seconds() from the timedelta you got from subtracting the two datetimes.
elapsed = (end_time-start_time).total_seconds()
print(f"You have ran the program for {elapsed} seconds, and will be charged {elapsed} cents.")
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a Python program which is running in a loop and downloading 20k RSS feeds using feedparser and inserting feed data into RDBMS.
I have observed that it starts from 20-30 feeds a min and gradually slows down. After couple of hours it comes down to 4-5 feeds an hour. If I kill the program and restart from where it left, again the throughput is 20-30 feeds a min.
It certainly is not MySQL which is slowing down.
What could be potential issues with the program?
In all likelihood the issue is to do with memory. You are probably holding the feeds in memory or somehow accumulating memory that isn't getting garbage collected. To diagnose:
Look at the size of your task (task manager if windows and top if unix/Linux) and monitor it as it grows with the feeds.
Then you can use a memory profiler to figure what exactly is consuming the memory
Once you have found that you can code differently maybe
A few tips:
Do an explicit garbage collection call (gc.collect()) after setting any relevant unused data structures to empty
Use a multiprocessing scheme where you spawn multiple processes that each handle a smaller number of feeds
Maybe go on a 64 bit system if you are using a 32 bit
Some suggestions on memory profiler:
https://pypi.python.org/pypi/memory_profiler
This one is quite good and the decorators are helpful
https://stackoverflow.com/a/110826/559095
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
leave out of cron command in unix.
Situations are bellow:
Users in total, about one million right now, and will grow to about 3 million in one year;
Timing tasks type: notification, calculation, upload data and so on;
Timing interval: from several minutes to one month;
Different tasks may have different logic and parameters;
requirements are bellow:
Better if can get it done in python, for the server code is Python;
The timing tolerance can be within 5 seconds, say if a task should be executed at 2015-01-01T00:00:00, it's ok to get it done from 2014-12-31T12:59:55 to 2015-01-01T00:00:05;
Log details for each task for each user, can debug in the future;
Can persist the task details info, for the server maybe down for some reasons;
If the server is down, can restart the tasks after re-firing up the server;
thanks a lot.
You could check fantail
You can create multiple Fantails to accommodate your different requirements and also see the Pickers
var sch = new Fantail({
debug: false, // Expose queues and handlers.
throttle: 200, // Run handlers (at most once) every 200 milliseconds
immediate: false // .start() immediately.
});
The schedule module is what you are looking for :
import schedule
import time
def foo():
print "Hello world !"
schedule.every(1).minutes.do(foo)
# You can do the following in another thread.
while True:
schedule.run_pending()
time.sleep(1)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to Python so be gentle.
I have been trying to write a program that counts or measures events in real time. At the moment I am using the sleep command but this pause event doesn't take into account the time the program takes to run. I have read up on the datetime module and can sort of see how this could be used, but I am a bit stuck in implementing this.
In short, I want a program that counts from 0 to 100 in real time seconds and milliseconds.
Your best bet (besides Googling for help before posting a question on SO) might be to do something like this:
Note the time when the program starts. start = datetime.datetime.now()
Do your calculations
sleep until 100 seconds after start. (start + datetime.timedelta(seconds=100))
Note that this won't be perfect, since there is a little overhead involved with the steps between accessing the "current time" and going to "sleep" (e.g. subtracting "current time" from "wake-up time"). However, if your sleep precision only needs to be in seconds, you should be okay.
Repeat as needed.
If, after trying it out, you need additional help with the actual implementation of these steps, feel free to come back and post another question on that topic.