如何在 Windows 中将 Python 脚本作为服务运行? [英] How do you run a Python script as a service in Windows?

查看:38
本文介绍了如何在 Windows 中将 Python 脚本作为服务运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一组共享存储在数据库中的各种相互关联对象的程序绘制架构草图.我希望其中一个程序充当服务,为对这些对象的操作提供更高级别的接口,而其他程序通过该服务访问这些对象.

I am sketching the architecture for a set of programs that share various interrelated objects stored in a database. I want one of the programs to act as a service which provides a higher level interface for operations on these objects, and the other programs to access the objects through that service.

我目前的目标是将 Python 和 Django 框架作为实现该服务的技术.我很确定我知道如何在 Linux 中守护 Python 程序.但是,系统应该支持 Windows 是一个可选的规范项.我几乎没有 Windows 编程经验,也完全没有 Windows 服务经验.

I am currently aiming for Python and the Django framework as the technologies to implement that service with. I'm pretty sure I figure how to daemonize the Python program in Linux. However, it is an optional spec item that the system should support Windows. I have little experience with Windows programming and no experience at all with Windows services.

是否可以将 Python 程序作为 Windows 服务运行(即无需用户登录即可自动运行)? 我不一定要实现这部分,但我需要一个粗略的想法这样做是为了决定是否按照这些路线进行设计.

Is it possible to run a Python programs as a Windows service (i. e. run it automatically without user login)? I won't necessarily have to implement this part, but I need a rough idea how it would be done in order to decide whether to design along these lines.

感谢到目前为止的所有答案,它们非常全面.我还想知道一件事:Windows 如何知道我的服务?我可以使用本机 Windows 实用程序管理它吗? 在/etc/init.d 中放置启动/停止脚本的等价物是什么?

推荐答案

是的,你可以.我使用 ActivePython 附带的 pythoncom 库来执行此操作,或者可以安装pywin32(适用于 Windows 扩展的 Python).

Yes you can. I do it using the pythoncom libraries that come included with ActivePython or can be installed with pywin32 (Python for Windows extensions).

这是一个简单服务的基本框架:

This is a basic skeleton for a simple service:

import win32serviceutil
import win32service
import win32event
import servicemanager
import socket


class AppServerSvc (win32serviceutil.ServiceFramework):
    _svc_name_ = "TestService"
    _svc_display_name_ = "Test Service"

    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):
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                              servicemanager.PYS_SERVICE_STARTED,
                              (self._svc_name_,''))
        self.main()

    def main(self):
        pass

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

您的代码将进入 main() 方法 - 通常带有某种无限循环,可能会通过检查您在 SvcStop 中设置的标志而中断方法

Your code would go in the main() method—usually with some kind of infinite loop that might be interrupted by checking a flag, which you set in the SvcStop method

这篇关于如何在 Windows 中将 Python 脚本作为服务运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆