TypeError:'NoneType'对象不可调用,但不明原因。有人能解释吗?
我想写一个Python脚本,定期检查一个服务。如果这个服务没有运行,它会自动启动。下面是我的代码:
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))
这是出现的错误:
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
我在想有没有人能帮我看看我哪里做错了。我还是Python的新手,但这不是我写的第一个程序。
1 个回答
2
你需要注册返回值,也就是service_info()
这个函数的结果;不过这个函数返回的是None
,也就是没有返回任何东西。
如果你想让调度器来调用service_info()
,那么你需要注册这个函数本身的引用,同时还要加上一个参数的元组:
run_periodically(time()+5, time()+10, 1, service_info, (action, machine, service))
然后你还需要调整run_periodically()
这个函数,让它能够接受这些参数,并把这些参数传递给scheduler.enterabs()
方法:
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()