Peter Pik
Peter Pik

Reputation: 11193

tornado periodiccallback fails on second run

I'm trying to make several PeriodicCallback's to monitor different parts of the server, however when i run the server it seem to run the PeriodicCallback the first time but that that, i get following error during next interval

File "/usr/local/lib/python3.6/site-packages/tornado/ioloop.py", line 830, in _run self.callback() TypeError: 'NoneType' object is not callable

When the server is running start_scheduler in server.py is being executed. why am i getting this error on the second run og the PeriodicCallback?

MonitorManager

class MonitorManager:

    instance = None

    @classmethod
    def get_instance(cls):
        if not cls.instance:
            cls.instance = cls()
        return cls.instance



    def __init__(self):

        self.node_scheduler = tornado.ioloop.PeriodicCallback(
            self.test(),
            1000*100)

    def test(self):
        print('done')

server.py

class SchedulerServer:

    VERSION = 'v1'

    singleton = None

    def __init__(self, scheduler_instance, monitor_instance):
        # Start scheduler
        self.scheduler_manager = scheduler_instance
        self.monitor_manager = monitor_instance

        self.tornado_settings = dict(
            debug=settings.DEBUG,
            static_path=settings.STATIC_DIR_PATH,
            template_path=settings.TEMPLATE_DIR_PATH,
            scheduler_manager=self.scheduler_manager,
            monitor_manager=self.monitor_manager
        )

        # Setup server
        URLS = [
            # Index page
            (r'/', index_page.Handler)
        ]
        self.application = tornado.web.Application(URLS, **self.tornado_settings)

    def start_scheduler(self):
        self.monitor_manager.start()
        self.scheduler_manager.start()

    def stop_scheduler(self):
        self.scheduler_manager.stop()

    @classmethod
    def run(cls):
        if not cls.singleton:

            cls.singleton = cls(
                scheduler_manager.SchedulerManager.get_instance(),
                monitor_manager.MonitorManager.get_instance()
            )
            cls.singleton.start_scheduler()

            cls.singleton.application.listen(settings.HTTP_PORT, settings.HTTP_ADDRESS)
            tornado.ioloop.IOLoop.instance().start()

Upvotes: 1

Views: 365

Answers (1)

Gennady Kandaurov
Gennady Kandaurov

Reputation: 1964

There is a mistake in PeriodicCallback call. PeriodicCallback requires a callback as the first argument, but here

self.node_scheduler = tornado.ioloop.PeriodicCallback(
            self.test(),
            1000*100)

there is a straight call of self.test(), which prints done and returns None, and this None is saved for future use of PeriodicCallback. So actually, there was an error on the first execution of PeriodicCallback, not on the second.

To fix it pass to PeriodicCallback any callable object, for example, self.test:

self.node_scheduler = tornado.ioloop.PeriodicCallback(
            self.test,
            1000*100)

Upvotes: 2

Related Questions