(python+luckysheet)封装为一个服务器启动程序

这一篇博客是紧跟着我的上一篇博客进行的。最后封装为一个服务器启动程序。

点击一个程序,实际启动的为三个程序,绿色部分显示为mysql(数据库),websocket(通信),exmysql(导出数据)。关闭程序,程序未关闭,在系统托盘显示,右键单击系统托盘图标,可以显示和退出,单击退出,服务器才退出,平时一直挂后台,如下图所示。

 

 

 

# coding: utf-8
import subprocess
import multiprocessing
import threading
import os ,time
import tkinter as tk
import psutil
import pystray
from pystray import MenuItem as item
from PIL import Image

def main():
    show_window()
    menu = (item('退出', quit_window), item('显示', show_window))
    image = Image.open("1.png")
    icon = pystray.Icon("name", image, "title", menu)
    icon.run()

def quit_window(icon, item):
    subprocess.run(["taskkill", "/im", "app.exe", "/f"])
    subprocess.run(["taskkill", "/im", "exmysql.exe", "/f"])
    icon.stop()

def show_window():
    global var
    window = tk.Tk()
    window.title('在线表格-服务器启动.exe')
    window.geometry('250x100')
    var = tk.StringVar()  # 定义一个字符串变量
    l = tk.Label(window, textvariable=var, bg='green', font=('宋体', 8), width=40, height=5, justify='center').pack()
    tk.Button(window, text='一键启动', width=15, height=2, command=thread1).pack()
    thread3()
    window.mainloop()

def fun1():
    #os.system('python ./app.py')
    #os.system('app.exe')
    subprocess.run('app.exe', shell=True, stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE)

def fun2():
    #os.system('python ./exmysql.py')
    #os.system('exmysql.exe')
    subprocess.run('exmysql.exe', shell=True, stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE)

def thread1():
    sing_process = multiprocessing.Process(target=fun1)
    dance_process = multiprocessing.Process(target=fun2)
    sing_process.start()
    dance_process.start()

def is_program_running(program_name):
    for p in psutil.process_iter():
        try:
            if program_name == p.name():
                return True
        except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
            pass
    return False

def refresh_window():
    list1 = []
    while True:
        if is_program_running('mysqld.exe'):
            list1.append('mysql已启动')
        else:
            list1.append('mysql未启动')
        if is_program_running('app.exe'):
            list1.append('websocket已启动')
        else:
            list1.append('websocket未启动')
        if is_program_running('exmysql.exe'):
            list1.append('exmysql已启动')
        else:
            list1.append('exmysql未启动')
        var.set(list1)
        list1.clear()
        time.sleep(3)

def thread3():
    thread = threading.Thread(target=refresh_window)
    thread.start()

if __name__ == '__main__':
    multiprocessing.freeze_support() # 窗口不弹窗报错
    main()

multiprocessing是进程,threading是线程。

psutil是为了检测后台程序是否存在,服务器后台挂掉了,可以快速定位。

pystray是为了后台挂的系统托盘。

值得注意的是os.system('app.exe'),cmd窗口一直存在,不美观,改为subprocess.run()。

再扯一句闲聊的话题,2023年多半要回成都工作了,不想再在外面漂了,人生苦短,我学python,早点回家吧。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

国服小闲鱼请战

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值