bottle 中的异步动作
Asynchronous action in bottle
我在瓶子里写了一个简单的应用程序。我需要每 10 秒 运行 相同的方法。我的第一个想法是这样的,但它不起作用,我认为这是一个丑陋的解决方案:
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",
你能建议我如何以正确的方式做到这一点吗?
可以使用threading模块调用Timer命令的方法:
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()
我在瓶子里写了一个简单的应用程序。我需要每 10 秒 运行 相同的方法。我的第一个想法是这样的,但它不起作用,我认为这是一个丑陋的解决方案:
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",
你能建议我如何以正确的方式做到这一点吗?
可以使用threading模块调用Timer命令的方法:
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()