Python Bottle中的通用循环

时间:2016-10-28 10:38:49

标签: python bottle

我正在使用Python Bottle框架。如何在Bottle主循环内每10秒执行一些操作?即在以下代码中调用npm install的位置?

worker_loop()

2 个答案:

答案 0 :(得分:1)

我建议使用schedule和一个单独的Thread来处理挂起的作业。你可以找到exmaple代码here。 如果您计划在线程上使用某些内容并使用uwsgi或类似内容,则应始终确保在配置中启用线程。

答案 1 :(得分:1)

这不是gevent解决的确切问题吗?如果您已下载模块,Bottle已经有一个适配器启动了gevent。

https://bottlepy.org/docs/dev/async.html

更新示例:

from gevent import monkey, spawn as gspawn, sleep as gsleep
monkey.patch_all()
from gevent.pywsgi import WSGIServer
import time
from bottle import route, run, template, view, request
import bottle

@route('/')
@view('test.html')
def index():
    context = {'request': request}
    return (context)

def worker_loop():
    work_on_the_db_each_10_seconds()
    gsleep(10)

gspawn(worker_loop)

app = bottle.app()
WSGIServer(("0.0.0.0", 9000), app).serve_forever()