Bartłomiej Bartnicki
Bartłomiej Bartnicki

Reputation: 1175

Asynchronous action in bottle

I have simple application in written in bottle. I need to run same method each 10 seconds. My first idea was something like this, but it is not working and I think it is ugly solution:

inc = 0
# after run server open /loop page in order do initiate loop 
@route('/loop', method='GET')
def whiletrue():
    global inc
    inc += 1
    print inc
    if inc != 1:
        return str(inc)
    while True:
        time.sleep(1)
        print "X",

Could you suggest me how to do it in right way?

Upvotes: 0

Views: 238

Answers (1)

Ari Gold
Ari Gold

Reputation: 1548

you can use the threading module to call the method with the Timer command:

from functools import partial
import threading

class While_True(threading.Thread):
     def __init__(self, **kwargs):
         threading.Thread.__init__(self)

     def whileTrue(self, *args):
         print args

     def caller(self, *args):
         threading.Timer(10, partial(self.whilTrue, "Hallo")).start()

Upvotes: 1

Related Questions