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

查看:304
本文介绍了如何在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中放置启动/停止脚本的等效性是什么? $ b

Thanks for all the answers so far, they are quite comprehensive. I would like to know one more thing: How is Windows aware of my service? Can I manage it with the native Windows utilities? What is the equivalent of putting a start/stop script in /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天全站免登陆