How to commit an action every minute in Python

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

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

time.sleep will not work in this situation because it pauses the entire program.

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

I tried to do something higher, but could not get him to constantly count. It only determines how long it will take to execute # some code.

Any help would be great! Thanks in advance.

+5
source share
4 answers

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

import thread, time, itertools

,

wood = itertools.count().next()

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

thread.start_new_thread(updateCounter, ())

ressource, , !

+7

, , , . ( , , .)

, , " ". , :

while game.running:
    game.process()

- - . - , - :

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

, . , , :

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

, , , - , . " ", "".

+5

, . , , , . . threading docs.

, , , .

+1

60 .

        try {
        while (true) {
            System.out.println(new Date());
            Thread.sleep(60 * 1000);
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
+1

All Articles