在子进程中启动Worker服务器
(tools_config, host, port, worker_id, master_url, log_file=None)
| 97 | |
| 98 | |
| 99 | def start_worker_process(tools_config, host, port, worker_id, master_url, log_file=None): |
| 100 | """在子进程中启动Worker服务器""" |
| 101 | # 设置无缓冲输出 |
| 102 | sys.stdout.reconfigure(line_buffering=True) if hasattr(sys.stdout, 'reconfigure') else None |
| 103 | sys.stderr.reconfigure(line_buffering=True) if hasattr(sys.stderr, 'reconfigure') else None |
| 104 | |
| 105 | # 重定向输出到日志文件(如果指定) |
| 106 | if log_file: |
| 107 | redirect_output_to_log(log_file, f"Worker-{worker_id}") |
| 108 | else: |
| 109 | # 如果没有指定日志文件,创建一个临时日志文件 |
| 110 | temp_log_file = f"/tmp/worker_{worker_id}.log" |
| 111 | try: |
| 112 | # 确保日志目录存在 |
| 113 | os.makedirs(os.path.dirname(temp_log_file), exist_ok=True) |
| 114 | # 创建日志文件 |
| 115 | with open(temp_log_file, 'w') as f: |
| 116 | timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S') |
| 117 | f.write(f"[{timestamp}] Worker {worker_id} 子进程启动\n") |
| 118 | |
| 119 | # 重定向到该日志文件,同时输出到控制台 |
| 120 | class TeeOutput: |
| 121 | def __init__(self, *files): |
| 122 | self.files = files |
| 123 | |
| 124 | def write(self, data): |
| 125 | if data: |
| 126 | data = sanitize_log_message(data) |
| 127 | for f in self.files: |
| 128 | f.write(data) |
| 129 | f.flush() |
| 130 | |
| 131 | def flush(self): |
| 132 | for f in self.files: |
| 133 | f.flush() |
| 134 | |
| 135 | log_file_obj = open(temp_log_file, 'a', buffering=1) # 行缓冲 |
| 136 | sys.stdout = TeeOutput(sys.__stdout__, log_file_obj) |
| 137 | sys.stderr = TeeOutput(sys.__stderr__, log_file_obj) |
| 138 | |
| 139 | print(f"📝 Worker {worker_id} 日志: {temp_log_file}") |
| 140 | except Exception as e: |
| 141 | print(f"⚠️ 无法创建日志文件 {temp_log_file}: {e}") |
| 142 | |
| 143 | worker = DistributedWorkerServer(tools_config, host, port, worker_id, master_url, log_file=log_file) |
| 144 | worker.run() |
| 145 | |
| 146 | |
| 147 | def create_merged_yaml_from_bootcamp_registry(bootcamp_registry_path, output_yaml_path): |
nothing calls this directly
no test coverage detected