Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an action happen every minute in Python

I would like to add 1 to a variable every minute without interfering with the code that's already running.

It is a game so I want in the background for wood+1, rock+1, fish+1 to happen every minute without the user knowing.

the time.sleep won't work in this situation because it pauses the whole program.

start=time.time()
#some code
end=time.time()
if end-start > 60:
    wood = wood+1
    rock = rock+1

I tried doing something above but could not get it to continuously count. It only measures how long it takes for #some code to be executed.

Any help would be great! Thanks in advance.

like image 231
MahoreLee Avatar asked Apr 03 '13 18:04

MahoreLee


People also ask

How do you repeat an action in Python?

The most common way to repeat a specific task or operation N times is by using the for loop in programming. We can iterate the code lines N times using the for loop with the range() function in Python.

How do you call a function every 5 minutes in Python?

With the help of the Schedule module, we can make a python script that will be executed in every given particular time interval. with this function schedule. every(5). minutes.do(func) function will call every 5 minutes.

How do I make a program run continuously in Python?

sleep() Function To Run Script repeatedly. So, if you do not want to use the above code and just want to run your script repeatedly then you can use the time. sleep() function. This function allows your script to keep running after sleeping for a certain amount of time.


4 Answers

You need to import the thread module and the itertools (in addition to svks' comment) in python

import thread, time, itertools

initialize your threadsafe counter like

wood = itertools.count().next()

and write your counter update into a function

def updateCounter():
   while True:
       wood.next()
       # wood += 1
       time.sleep(60)

and kick off a new thread with

thread.start_new_thread(updateCounter, ())

But the ressource variables like wood must be accessable from within the counter function!

like image 123
thomas Avatar answered Oct 01 '22 01:10

thomas


Here's an answer that does not involve threads, because threads can be tricky to get right. (If you use threads you need to read up on synchronization between threads, or you can risk running into subtle bugs.)

You probably have some sort of "main loop". Maybe it looks something like this, currently:

while game.running:
    game.process()

There's nothing magical about the main loop -- you can do things besides processing input and painting to the screen here. If you want something to run once every minute you can simply do something like this:

t0 = time.time()
while game.running:
    t1 = time.time()
    if (t1-t0) >= 60.0:
        game.wood += 1
        t0 = t1 
    game.process()

If you're using certain libraries you may have your main loop hidden away inside the library. In this case you're probably starting your game more or less like this:

game.main() # this function doesn't return until your game exits

If this is the case, there will probably be a function in your library to do something equivalent to the above -- setting code to execute either every so often or at specified intervals. Look for either "idle functions" or "timers".

like image 27
svk Avatar answered Oct 01 '22 00:10

svk


To do this you require separate threads which will be responsible for incrementing the counters. But please remember that introducing threads to any program makes it more complicated. Please refer to threading docs for details.

Also you aren't certain that particular thread will get processor's time each second, you can assume that but you aren't certain.

like image 24
Marcin Pietraszek Avatar answered Oct 01 '22 00:10

Marcin Pietraszek


it will run every 60 seconds.

        try {
        while (true) {
            System.out.println(new Date());
            Thread.sleep(60 * 1000);
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
like image 39
Mitul Maheshwari Avatar answered Oct 01 '22 01:10

Mitul Maheshwari