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

2022-09-11 17:18:41 浏览数 (1)

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

  • 附上redis安装目录结构
  • 附上程序代码
代码语言:javascript复制
# 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_proExtensions\redis3.0.504config\'   f   '>>D:devpythonlogs\'   tags   '_log.txt 2>&1'
    os.system(command_str)


def file_name(file_dir, _names):
    # type: (str) -> str
    system = str('D:phpstudy_proExtensions\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_proExtensions\redis3.0.504config\')
    name = str('redis-'   port   '.conf')
    names.append(name)
    content = str('bind 127.0.0.1nport '   port   'ntimeout 65nmaxclients 10000ndatabases 16nmaxmemory 1048576000')
    makefile(path, name, content)

conf = 'D:phpstudy_proExtensions\redis3.0.504config\'

file_name(conf, names)

0 人点赞