如何在 CherryPy 中定期执行一个函数

How to exec periodically a function in CherryPy

此代码使用 CheryPy 生成 3 个网页。它有效,但现在我需要定期执行函数 "PageWeb" 以获取查询的最新信息。 如何在 CherryPy 中使用线程:

from Widget import showLine
from Widget import SocketLivestatus
import cherrypy
import threading


def PageWeb(cluster):
    # Open Socket
    table = SocketLivestatus(['host1','host2'], 50000, cluster)
    # Result
    Line = showLine(table) 
    HTML = '''<!doctype html>
             <html lang="en">
                <head><meta charset="utf-8">
                 <title>Widget</title>
                 <link rel="stylesheet" href="css/font-awesome.min.css">
             </head>
            <body style="background-color: #1F1F1F">'''+Line+'''</body>
            </html>'''
    return HTML

#BEFORE
re7 = PageWeb("re7")
prod1 = PageWeb("prod1")
prod2 = PageWeb("prod2")
#MY GOAL
re7 = threading.Timer(5.0, PageWeb("re7")).start()
prod1 = threading.Timer(5.0, PageWeb("prod1")).start()
prod2 = threading.Timer(5.0, PageWeb("prod2")).start()


class HelloWorld(object):
    @cherrypy.expose
    def re7(self):
        return re7

    @cherrypy.expose
    def prod1(self):
        return prod1

    @cherrypy.expose
    def prod2(self):
        return prod2



if __name__ == '__main__':
   cherrypy.config.update(
    {'server.socket_host': '0.0.0.0'} )
   cherrypy.quickstart(HelloWorld(),config={
        '/':
        { 'tools.staticdir.on':True,
          'tools.staticdir.dir': "/app"
        }
       # '/fonts':
       # { 'tools.staticdir.on':True,
       #   'tools.staticdir.dir': "/app"
       # }  
    })

问题是关于 threading.Timer(5.0, PageWeb("...")).start() return 一个错误:

Traceback (most recent call last):
  File "/usr/lib64/python2.7/threading.py", line 811, in __bootstrap_inner
    self.run()
  File "/usr/lib64/python2.7/threading.py", line 1083, in run
    self.function(*self.args, **self.kwargs)
TypeError: 'str' object is not callable

我想要帮助使用 CherryPy 中的线程函数。

一个threading.Timer只运行一次:

This class represents an action that should be run only after a certain amount of time has passed

经过一些试验,cherrypy 似乎不能很好地处理线程。以下代码使用 multiprocessing 库创建一个单独的 ProcessHTML 通过将其存储为托管 list:

的第一个条目来共享
import cherrypy
import multiprocessing
import time

def PageWeb(cluster, sleep_interval, lock, shared_result):
    counter = 0
    while True:
        HTML = '''<!doctype html>
                 <html lang="en">
                    <head><meta charset="utf-8">
                     <title>Widget</title>
                     <link rel="stylesheet" href="css/font-awesome.min.css">
                 </head>
                <body style="background-color: #1F1F1F">'''+str(counter)+'''</body>
                </html>'''
        counter += 1
        with lock:
            shared_result[0] = HTML
        time.sleep(sleep_interval)

class HelloWorld(object):
    def __init__(self):
        self.re7_lock = multiprocessing.Lock()
        self.manager = multiprocessing.Manager()
        self.re7_result = self.manager.list()
        self.re7_result.append('')
        arg_list = ("re7", 5.0, self.re7_lock, self.re7_result)
        self.re7_process = multiprocessing.Process(target=PageWeb, args=arg_list)
        self.re7_process.daemon = True
        self.re7_process.start()

    @cherrypy.expose
    def re7(self):
        with self.re7_lock:
            return str(self.re7_result[0])

cherrypy.quickstart(HelloWorld())

此代码是 minimal, complete, and verifiable example。您应该能够将它与您的代码集成。