我试图将python作为Windows服务运行。
以下这篇文章工作正常:https://gist.github.com/guillaumevincent/d8d94a0a44a7ec13def7f96bfb713d3f
但是当我尝试导入一些我的其他python并从这个服务调用时它不会工作。
服务代码:
import servicemanager
import socket
import sys
import win32event
import win32service
import win32serviceutil
import datetime
# impoort own module
from modules import copy_log
class TestService(win32serviceutil.ServiceFramework):
_svc_name_ = "TestService7"
_svc_display_name_ = "Test Service7"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
socket.setdefaulttimeout(60)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
rc = None
while rc != win32event.WAIT_OBJECT_0:
now = datetime.datetime.now()
with open('C:\\test\\TestService.log', 'a') as f:
self.count = self.count + 1
f.write(str(self.count) + ' :: '+str(now) + '\n')
f.write('________________\n')
f.write('test service running...\n')
#call own module
"Here is simple function im my seprare python project and is not working"
copy_log.CopyTest()
rc = win32event.WaitForSingleObject(self.hWaitStop, 1000*10)
def main(self):
pass
if __name__ == '__main__':
if len(sys.argv) == 1:
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(TestService)
servicemanager.StartServiceCtrlDispatcher()
else:
win32serviceutil.HandleCommandLine(TestService)
python服务调用的文件/模块示例:
import datetime
from shutil import copyfile
class CopyTest(object):
def __init__(self):
print(self.__str__())
pass
def copy_it(self):
copyfile("C://test_read/aaa.txt",
"C:///test_write/aaa.txt")
并没有那样做。我不知道为什么?