WarMachine
WarMachine

Reputation: 25

TypeError: 'NoneType' object is not callable, but not sure why. Can someone please explain?

Trying to create a python script to do a periodic check on a service. If it is not running, it will auto-start it. Here is the code:

import win32serviceutil
from sched import scheduler
from time import time, sleep

s = scheduler(time, sleep)

def run_periodically(start, end, interval, func):
    event_time = start
    while event_time < end:
        s.enterabs(event_time, 0, func, ())
        event_time += interval

    s.run()



if __name__ == '__main__':    
    machine = 'George'
    service = 'Hamachi2Svc'
    action = 'start'        

    def service_info(action, machine, service):
            if win32serviceutil.QueryServiceStatus(service, machine)[1] == 4:
                print "%s is currently running" % service 
            else:
                print "%s is *not* running" % service
                print "%s is starting..." % service
                win32serviceutil.StartService(service, machine)
                print '%s started successfully' % service

    run_periodically(time()+5, time()+10, 1, service_info(action, machine, service))

Here is the error produced:

Traceback (most recent call last):
  File "C:\Python27\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 323, in RunScript
    debugger.run(codeObject, __main__.__dict__, start_stepping=0)
  File "C:\Python27\Lib\site-packages\pythonwin\pywin\debugger\__init__.py", line 60, in run
    _GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
  File "C:\Python27\Lib\site-packages\pythonwin\pywin\debugger\debugger.py", line 655, in run
    exec cmd in globals, locals
  File "C:\Users\Administrator\Desktop\HamachiDestroyerV.01.py", line 1, in <module>
    import win32serviceutil
  File "C:\Users\Administrator\Desktop\HamachiDestroyerV.01.py", line 13, in run_periodically
    s.run()
  File "C:\Python27\lib\sched.py", line 117, in run
    action(*argument)
TypeError: 'NoneType' object is not callable

I was wondering if anyone could lend their expertise and inform me what I'm doing wrong. I'm still a beginner in python, but this isn't the first program I have made.

Upvotes: 2

Views: 2795

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122312

You register the return value of service_info(); that function returns None.

If you wanted service_info() to be called by the scheduler instead, you'll need to register the reference to the function itself, together with a tuple of arguments:

run_periodically(time()+5, time()+10, 1, service_info, (action, machine, service))

and adjust run_periodically() to accept the arguments and pass that on to the scheduler.enterabs() method:

def run_periodically(start, end, interval, func, args=()):
    event_time = start
    while event_time < end:
        s.enterabs(event_time, 0, func, args)
        event_time += interval

    s.run()

Upvotes: 2

Related Questions