gpt4 book ai didi

python win32 : error

转载 作者:太空宇宙 更新时间:2023-11-04 00:53:14 25 4
gpt4 key购买 nike

我正在尝试让 python 脚本作为 Windows 服务运行。

只是为了有一点理解,我从 here 中获取了这段代码.

我已经安装了以下东西:

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

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

  1. win32event.CreateEvent
  2. win32event.WAIT_OBJECT_0
  3. win32event.WaitForSingleObject(self.hWaitStop, 5000)
  4. win32service.SERVICE_STOP_PENDING)
  5. win32event.SetEvent

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

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)

最佳答案

我试图从 sourceforge.net 站点重新安装 win32。但没有成功。

最后我尝试了 pip 命令,它非常有效。

pip install pypiwin32

关于 python win32 : error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36187311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com