2023년 11월 22일 수요일

Python을 Windows service로 실행하기

pywin32 설치

python -m pip install --upgrade pywin32 

- 가상환경에서 설치하면 안됨.


가상 환경 외부에서는 COM 객체, 서비스 등을 설치하고자 할 때

python Script/pywin32_postinstall.py -install


[myservice.py]

import win32serviceutil
import ctypes
import time

OutputDebugString = ctypes.windll.kernel32.OutputDebugStringW


class MyService(win32serviceutil.ServiceFramework):
_svc_name_ = 'MyService'
_svc_display_name_ = 'My Service'
is_running = False

def SvcStop(self):
OutputDebugString("MyService __SvcStop__")
self.is_running = False

def SvcDoRun(self):
self.is_running = True
while self.is_running:
OutputDebugString("MyService __loop__")
time.sleep(1)


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


관리자 권한으로 PowerSell 실행

python myservice.py install

python myservice.py start

python myservice.py stop

[python] subprocess

proc.py import asyncio from argparse import ArgumentParser from asyncio import sleep async def process_function(user, file_path):     if use...