简介
新公司都是在windows办公,尝试了将好用的jupyterlab注册成服务,成功后记录一下
步骤
安装依赖pywin32
在要做成notebook根目录的目录下创建install_service.py文件
import win32serviceutil
import win32service
import win32event
import servicemanager
import win32process
from win32process import CreateProcess
import os
import socket
import traceback
import getpass
import time
work_dir = os.path.abspath(os.path.dirname(__file__))
jupyterlab_path = r'E:\python38\Scripts\jupyter-lab.exe'
class Service(win32serviceutil.ServiceFramework):
_svc_name_ = "JupyterLab"
_svc_display_name_ = "JupyterLab Service"
_svc_description_ = "JupyterLab Service"
def __init__(self, *args):
os.chdir(work_dir)
super().__init__(*args)
self.hWaitStop = win32event.CreateEvent(None,0,0,None)
socket.setdefaulttimeout(60)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
rc = win32event.WaitForSingleObject(self.handle[0], 1000)
if not rc == win32event.WAIT_OBJECT_0:
win32process.TerminateProcess(self.handle[0],0)
self.ReportServiceStatus(win32service.SERVICE_STOPPED)
win32event.setEvent(self.hWaiStop)
def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
self.handle = CreateProcess(jupyterlab_path, '', None, None, 0,
win32process.CREATE_NO_WINDOW, None, None,
win32process.STARTUPINFO())
rc = win32event.WaitForSingleObject(self.handle[0], 2)
self.running = True
# time.sleep(100)
while self.running:
rc = win32event.WaitForSingleObject(self.handle[0], 1000)
if rc == win32event.WAIT_OBJECT_0 or rc == win32event.WAIT_FAILED:
self.running = False
time.sleep(3)
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(Service)
- 记得修改文件中的jupyterlab_path为你本机实际地址
在有管理员权限的powershell中执行
python install_service.py --username ".\你windows用户名" --password "你windows密码" --startup=auto install
- 如果要删除、更新就将install 改为remove、delete
- 一定要停止服务之后再删除,不然需要重启电脑才可以重新安装