在Python中结合文本使用APscheduler

vyswwuz2  于 2023-02-07  发布在  Python
关注(0)|答案(1)|浏览(105)

我需要一个调度器在文本应用程序定期查询外部数据源。作为测试,我已经尝试使用APscheduler调用tick()函数每秒钟。
然而,尽管应该启动调度程序,但什么也没发生。
这是怎么回事?如何调试?

from textual.app import App, ComposeResult
from textual.containers import Horizontal, Vertical
from textual.widgets import *

from apscheduler.schedulers.background import BackgroundScheduler
 

class HeaderApp(App):
    def __init__(self, *args, **kwargs):
        self.sched = BackgroundScheduler()
        self.sched.add_job(self.tick,'interval', seconds=1)
        self.sched.start()
        super(HeaderApp, self).__init__(*args, **kwargs)
    
    def compose(self) -> ComposeResult:
        yield Header()
        yield TextLog()
        
    def tick(self):
        text_log = self.query_one(TextLog)
        text_log.write("tick")
        
    def on_mount(self):
        text_log = self.query_one(TextLog)
        text_log.write(self.sched.running)
        
if __name__ == "__main__":
    app = HeaderApp()
    app.run()
gcuhipw9

gcuhipw91#

我不熟悉apscheduler,但是既然你还没有得到响应,你能使用内置的set_interval定期调用一个方法吗?

from textual.app import App, ComposeResult
from textual.containers import Horizontal, Vertical
from textual.widgets import *

class HeaderApp(App):
    def compose(self) -> ComposeResult:
        yield Header()
        yield TextLog()

    def tick(self):
        text_log = self.query_one(TextLog)
        text_log.write("tick")

    def on_mount(self):
        self.set_interval(1, self.tick)

if __name__ == "__main__":
    app = HeaderApp()
    app.run()

相关问题