Python win32:

2024-09-27 07:20:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图让python脚本作为windows服务运行。在

为了稍微了解一下,我从here获取了这段代码。在

我安装了以下设备:

  1. python2.7(64位)
  2. pywin32.exe。(64位)

但是,脚本无法找到以下内容:

  1. win32事件.CreateEvent在
  2. win32等待对象\u在
  3. win32event.WaitForSingleObject(self.hWaitStop公司,5000)
  4. win32service.service_STOP_挂起)在
  5. win32事件.设置事件在

虽然我没有收到导入的任何错误消息,但我收到了上述函数的错误消息。在

import win32service  
import win32serviceutil  
import win32event  

class PySvc(win32serviceutil.ServiceFramework):  
# you can NET START/STOP the service by the following name  
_svc_name_ = "PySvc"  
# this text shows up as the service name in the Service  
# Control Manager (SCM)  
_svc_display_name_ = "Python Test Service"  
# this text shows up as the description in the SCM  
_svc_description_ = "This service writes stuff to a file"  

def __init__(self, args):  
    win32serviceutil.ServiceFramework.__init__(self,args)  
    # create an event to listen for stop requests on  
    self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)  

# core logic of the service     
def SvcDoRun(self):  
    import servicemanager  

    f = open('test.dat', 'w+')  
    rc = None  

    # if the stop event hasn't been fired keep looping  
    while rc != win32event.WAIT_OBJECT_0:  
        f.write('TEST DATA\n')  
        f.flush()  
        # block for 5 seconds and listen for a stop event  
        rc = win32event.WaitForSingleObject(self.hWaitStop, 5000)  

    f.write('SHUTTING DOWN\n')  
    f.close()  

# called when we're being shut down      
def SvcStop(self):  
    # tell the SCM we're shutting down  
    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)  
    # fire the stop event  
    win32event.SetEvent(self.hWaitStop)  

if __name__ == '__main__':  
    win32serviceutil.HandleCommandLine(PySvc)

Tags: thenameimportselfeventserviceutil事件

热门问题