python写的游戏怎么在windows运行_如何在Windows中将Python脚本作为服务运行?

是的你可以.我使用ActivePython附带的pythoncom库或者可以安装pywin32(Python for Windows扩展)来实现.

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

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方法中设置的标志来中断

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值