Python写程序要求有两台服务器,一台为主服务器,另一台为副服务器。 主服务器上有8个根目录,上千万个子目录,一共有两亿个文件。要求把主服务器上的文件同步到副服务器上,要求使用while死循环实时运行监控主服务器是否有新文件产生。如果副服务器的文件已经存在则用CRC比较两个文件是否一致,如果不一致则拷贝,如果一致则不拷贝。要求使用tkinter 自定义8个选择源路径和目标路径的选项,选择路径后点击"开始"按钮运行,点击"取消"按钮结束运行。

时间: 2024-03-19 12:45:00 浏览: 15
以下是一个可能的Python程序实现: ```python import tkinter as tk import threading import queue import os import zlib class Application(tk.Frame): def __init__(self, master=None): super().__init__(master) self.master = master self.queue = queue.Queue() self.threads = [] self.create_widgets() def create_widgets(self): # 创建8个选择源路径和目标路径的选项 self.source_path_labels = [] self.source_path_entries = [] self.target_path_labels = [] self.target_path_entries = [] for i in range(8): source_path_label = tk.Label(self.master, text=f"源路径{i+1}:") source_path_entry = tk.Entry(self.master) target_path_label = tk.Label(self.master, text=f"目标路径{i+1}:") target_path_entry = tk.Entry(self.master) source_path_label.grid(row=i, column=0, sticky=tk.W) source_path_entry.grid(row=i, column=1) target_path_label.grid(row=i, column=2, sticky=tk.W) target_path_entry.grid(row=i, column=3) self.source_path_labels.append(source_path_label) self.source_path_entries.append(source_path_entry) self.target_path_labels.append(target_path_label) self.target_path_entries.append(target_path_entry) # 创建开始按钮和取消按钮 self.start_button = tk.Button(self.master, text="开始", command=self.start) self.start_button.grid(row=8, column=1) self.cancel_button = tk.Button(self.master, text="取消", command=self.cancel) self.cancel_button.grid(row=8, column=3) def start(self): # 开始运行 for i in range(8): source_path = self.source_path_entries[i].get() target_path = self.target_path_entries[i].get() if not source_path or not target_path: continue t = threading.Thread(target=self.sync_files, args=(source_path, target_path)) self.threads.append(t) t.start() self.monitor_files() def monitor_files(self): # 监控主服务器是否有新文件产生 while True: for root_dir in root_dirs: for root, dirs, files in os.walk(root_dir): for file in files: file_path = os.path.join(root, file) self.queue.put(file_path) def sync_files(self, source_path, target_path): # 同步文件 while True: try: file_path = self.queue.get(timeout=1) except queue.Empty: continue if os.path.isfile(file_path): relative_path = os.path.relpath(file_path, source_path) target_file_path = os.path.join(target_path, relative_path) if os.path.isfile(target_file_path): if self.check_crc(file_path, target_file_path): continue os.makedirs(os.path.dirname(target_file_path), exist_ok=True) with open(file_path, "rb") as f1, open(target_file_path, "wb") as f2: f2.write(f1.read()) def check_crc(self, file_path1, file_path2): # 使用CRC比较两个文件是否一致 with open(file_path1, "rb") as f1, open(file_path2, "rb") as f2: crc1 = zlib.crc32(f1.read()) crc2 = zlib.crc32(f2.read()) return crc1 == crc2 def cancel(self): # 结束所有线程 for t in self.threads: t.join() self.master.quit() if __name__ == "__main__": # 主服务器上的8个根目录 root_dirs = ["/root/dir1", "/root/dir2", "/root/dir3", "/root/dir4", "/root/dir5", "/root/dir6", "/root/dir7", "/root/dir8"] # 创建tkinter应用程序 root = tk.Tk() app = Application(master=root) app.mainloop() ``` 在上面的实现中,首先创建了一个`Application`类,使用`tkinter`创建了8个选择源路径和目标路径的选项,以及开始和取消按钮。点击开始按钮后,会开启一个主线程和8个子线程,主线程使用while死循环实时运行监控主服务器是否有新文件产生,如果有新文件产生则将文件路径放入一个队列中,子线程从队列中取出文件路径进行同步。子线程使用CRC比较两个文件是否一致,如果不一致则拷贝,如果一致则不拷贝。点击取消按钮后,结束所有线程的运行。

相关推荐

最新推荐

recommend-type

python分割一个文本为多个文本的方法

主要为大家详细介绍了python分割一个文本为多个文本,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

在服务器上安装python3.8.2环境的教程详解

主要介绍了在服务器上安装python3.8.2环境的教程详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

python服务器与android客户端socket通信实例

主要介绍了python服务器与android客户端socket通信的实现方法,实例形式详细讲述了Python的服务器端实现原理与方法,以及对应的Android客户端实现方法,需要的朋友可以参考下
recommend-type

python使用socket创建tcp服务器和客户端

主要为大家详细介绍了python使用socket创建tcp服务器和客户端,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

python实现从ftp服务器下载文件

主要为大家详细介绍了python实现从ftp服务器下载文件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。