1

I have a python flask application that starts running with a variable called 'counter' initialized as 0. I want to increment this variable everyday at 00:30 AM and print it to the python console, without disturbing the application run.

I may want to pass that variable's current value at any point to the client.

counter = 0

if __name__ == "__main__":
   port = 80
   os.system("open http://localhost:{0}".format(port))

   app.debug = False
   app.run(host='0.0.0.0', port=port)

   #### NEED CODE FOR HERE ####
   #Trying out logic:
   if (time == 00:30):
       counter = counter+1
       print counter
   #### NEED CODE FOR HERE ####

Please provide working code in your answer.

user3422637
  • 3,967
  • 17
  • 49
  • 72
  • Any code after app.run() won't run. What are you trying to accomplish? There is probably a better way to do it using date math – nathancahill May 12 '15 at 18:54
  • I guess this can be done using multi-threading. I don't know how to do that. – user3422637 May 12 '15 at 18:57
  • If you're actually trying to run some code everyday at 00:30 AM, look at [celery beat](http://celery.readthedocs.org/en/latest/userguide/periodic-tasks.html) – nathancahill May 12 '15 at 18:58
  • What operating system are you using? You could try a cron job in Linux and other *nix variants – Jesuisme May 12 '15 at 20:17

2 Answers2

2

There are several solutions in other answers, depending on how complex your requirements are. If all you really need to do is increment a counter, then use the first approach.

If you have the ability to schedule cron jobs on your server, the simplest approach by far is to define a URL that performs the action, and have a cron job that runs curl or wget to request (with POST) the URL and trigger the action at the required time. This requires no additional packages and is very lightweight. Much as described here:

You could possibly use a background thread, which in your case would need to sleep or set a timer to wake up at the required time:

One of the most popular options is to use Celery for background task processing. This is rather heavyweight as it also requires RabbitMQ, but is the most flexible and powerful solution:

There's Python Redis-Queue, which requires the Redis backend:

Some good ideas on Reddit also:

An overview of task queues for Python:

Community
  • 1
  • 1
gavinb
  • 19,278
  • 3
  • 45
  • 60
0

The easiest way to do this is to not have a counter at all, and instead calculate the value of the counter dynamically at the time it is needed.

For example, if you want your counter to start at 0 on May 12, 2015 0:30am, you can get the counter value for any time that follows with this code:

import datetime

start_counter = datetime.datetime(2015, 5, 12, 0, 30)

def get_counter(when):
    return (when - start_counter).days

The following examples show the value of the counter at different times:

>>> get_counter(datetime.datetime(2015,5,12,0,45))  # May 12th, 0:45am
0
>>> get_counter(datetime.datetime(2015,5,13,0,45))  # May 13th, 0:45am
1
>>> get_counter(datetime.datetime(2015,5,14,0,15))  # May 14th, 0:15am
1
>>> get_counter(datetime.datetime(2015,5,14,0,45))  # May 14th, 0:45am
2

I hope this helps!

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
  • but I want the function to be called by itself even while the flask application is running – user3422637 May 12 '15 at 20:23
  • Does it do anything more than increment a counter? If that's all it does you do not need to call the function. If you need to do something else, then you need to explain that in your question. – Miguel Grinberg May 12 '15 at 20:44
  • I am just trying to understand the concept, so that I could do any operation on a timed schedule even when the app is running – user3422637 May 12 '15 at 20:45
  • Like the comments on your question say, look at running a cron job or celery beat. – nathancahill May 12 '15 at 22:50