小编tas*_*tas的帖子

无法启动用Python编写的Windows服务(win32serviceutil)

我正在尝试启动一个简单的服务示例:

someservice.py:

import win32serviceutil 
import win32service 
import win32event

class SmallestPythonService(win32serviceutil.ServiceFramework):
    _svc_name_ = "SmallestPythonService"
    _svc_display_name_ = "display service"

    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
        win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)

if __name__=='__main__':
    win32serviceutil.HandleCommandLine(SmallestPythonService)
Run Code Online (Sandbox Code Playgroud)

我跑的时候

python someservice.py install
Run Code Online (Sandbox Code Playgroud)

一切都很好,服务出现在Windows服务列表中,但是

python someservice.py start
Run Code Online (Sandbox Code Playgroud)

失败,出现"错误1053:服务未及时响应启动或控制请求",但没有任何延迟.

我用谷歌搜索了一个解决方案,它表示pythonservice.exe无法找到时会发生这种情况python27.dll.它实际上不可能,所以我说C:\Python27PATH.现在pythonservice.exe运行正常,但错误1053仍然存在.

我在Windows 7旗舰版上使用pywin32 216运行Python 2.7.2并具有管理员权限.

python windows windows-services pywin32

17
推荐指数
3
解决办法
2万
查看次数

du_util.copy_tree在shutil.rmtree之后失败

我在尝试将文件夹删除后将其复制到另一个文件夹:

for i in range(0,3):
   try:
      dir_util.remove_tree("D:/test2")
 #     shutil.rmtree("D:/test2")
      print "removed"
   except: pass

   dir_util.copy_tree("D:/test1", "D:/test2")

   print i
Run Code Online (Sandbox Code Playgroud)

D:/ test1包含一个名为test_file的空文件.如果我使用dir_util.remove_tree它工作正常,但在shutil.rmtree之后它只能工作一次,在第二次迭代时它会失败.输出:

removed
0
removed
Traceback (most recent call last):
  File "test.py", line 53, in <module>
    dir_util.copy_tree("D:/test1", "D:/test2")
  File "C:\Python27\lib\distutils\dir_util.py", line 163, in copy_tree
    dry_run=dry_run)
  File "C:\Python27\lib\distutils\file_util.py", line 148, in copy_file
    _copy_file_contents(src, dst)
  File "C:\Python27\lib\distutils\file_util.py", line 44, in _copy_file_contents
    fdst = open(dst, 'wb')
IOError: [Errno 2] No such file or directory: 'D:/test2\\test_file'
Run Code Online (Sandbox Code Playgroud)

使用shutil.rmtree比较方便,因为它允许删除只读文件的错误处理.dir_util.remove_tree和shutil.rmtree有什么区别?为什么copyttree在rmtree第二次之后没有工作?

我在Windows 7上运行Python 2.7.2

python

8
推荐指数
3
解决办法
2869
查看次数

标签 统计

python ×2

pywin32 ×1

windows ×1

windows-services ×1