Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何创建一个windows服务的python脚本,当脚本有很多依赖文件时?_Python_Windows_Pywin32 - Fatal编程技术网

如何创建一个windows服务的python脚本,当脚本有很多依赖文件时?

如何创建一个windows服务的python脚本,当脚本有很多依赖文件时?,python,windows,pywin32,Python,Windows,Pywin32,我提到了创建python windows服务。如果我有此服务的dependecy python_read_email.py文件,我不确定这将如何工作。如何创建这样的服务,我有一堆依赖的文件 import win32service import win32serviceutil import win32event import python_read_email class PySvc(win32serviceutil.ServiceFramework): # you can NET

我提到了创建python windows服务。如果我有此服务的dependecy python_read_email.py文件,我不确定这将如何工作。如何创建这样的服务,我有一堆依赖的文件

import win32service
import win32serviceutil
import win32event
import python_read_email


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

        rc = None

        # if the stop event hasn't been fired keep looping
        while rc != win32event.WAIT_OBJECT_0:
            python_read_email.run()
            # block for 5 seconds and listen for a stop event
            rc = win32event.WaitForSingleObject(self.hWaitStop, 5000)

    # 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)

我更喜欢将其作为“计划任务”运行(特别是当您的代码不是线程安全的和/或需要在单个线程中运行时),这可能会有所帮助:您写道:“我不确定这将如何工作……”。你试过了吗?有问题吗?是的,我试图创建一个服务,但它没有按预期运行。