0

i need a scheduler in a Textual application to periodically query an external data source. As a test i've tried to use APscheduler to call a tick() function every second.

However nothing happens although the scheduler should be started.

What is going on and how to debug this?

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()
Marvin Noll
  • 585
  • 1
  • 7
  • 23

1 Answers1

0

I'm not familiar with apscheduler, but since you haven't had a response, could you use the builtin set_interval which will call a method at regular intervals?

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()
Will McGugan
  • 2,005
  • 13
  • 10