zl程序教程

您现在的位置是:首页 >  后端

当前栏目

windows 下python 脚本启动多个redis服务

PythonRedisWindows服务 启动 脚本 多个
2023-06-13 09:12:24 时间

刚开始用bat脚本写的批量启动,但是一直卡到第一个redis启动,查询尝试无果,果断用python写了一个简单的脚本。

  • 附上redis安装目录结构
  • 附上程序代码
# python >=2.x
import os
import threading


def makefile(_path, _name, _content):
    # type: (str, str, str) -> str
    filer = _path + _name
    if not os.path.exists(_path):
        print('the path is not exists')
    else:
        if not os.path.isdir(_path):
            print('the dir name is not exists')
        else:
            if not os.path.isfile(filer):
                f = open(filer, 'w+')
                f.write(_content)
                f.seek(0)
            else:
                print(filer + ' exists')


def execute_cmd_command(command_str, f, tags):
    command_str = command_str + ' D:\phpstudy_pro\Extensions\\redis3.0.504\config\\' + f + '>>D:\dev\python\logs\\' + tags + '_log.txt 2>&1'
    os.system(command_str)


def file_name(file_dir, _names):
    # type: (str) -> str
    system = str('D:\phpstudy_pro\Extensions\\redis3.0.504\\redis-server.exe')  # type: str
    if not os.path.isfile(system):
        pass
    else:
        for root, dirs, files in os.walk(file_dir):
            for f in files:
                if str(os.path.splitext(f)[0]) + str(os.path.splitext(f)[1]) in _names:
                    if os.path.splitext(f)[1] == '.conf':
                        t = threading.Thread(target=execute_cmd_command, args=(system, f, os.path.splitext(f)[0]))
                        t.start()


default = '6379,6380,6381'  # type: str

source = input('Enter your input eg(6379,6380,6381): ')

source = default if len(source) == 0 else source

ports = source.split(',')

names = []
for port in ports:
    path = str('D:\phpstudy_pro\Extensions\\redis3.0.504\config\\')
    name = str('redis-' + port + '.conf')
    names.append(name)
    content = str('bind 127.0.0.1\nport ' + port + '\ntimeout 65\nmaxclients 10000\ndatabases 16\nmaxmemory 1048576000')
    makefile(path, name, content)

conf = 'D:\phpstudy_pro\Extensions\\redis3.0.504\config\\'

file_name(conf, names)