-
-
Save cccaballero/83a7bd86c05b2f3f6497 to your computer and use it in GitHub Desktop.
copyfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import time | |
import os | |
import multiprocessing | |
SRC_FILE = '/home/myuser/test/cp/orig/a.zip' | |
DST_FILE = '/home/myuser/test/cp/1/a.zip' | |
try: | |
O_BINARY = os.O_BINARY | |
except: | |
O_BINARY = 0 | |
READ_FLAGS = os.O_RDONLY | O_BINARY | |
WRITE_FLAGS = os.O_WRONLY | os.O_CREAT | os.O_TRUNC | O_BINARY | |
BUFFER_SIZE = 128*1024 | |
def copy_file(src, dst=[], progress=None, only_new_file=True): | |
out_files = dict.fromkeys(dst) | |
if only_new_file: | |
# copy only newer files | |
out_files = [] | |
src_modified = time.ctime(os.path.getmtime(src)) | |
for dest in dst: | |
if os.path.exists(dest): | |
dest_modified = time.ctime(os.path.getmtime(dest)) | |
if src_modified > dest_modified: | |
out_files.append(dest) | |
else: | |
out_files.append(dest) | |
out_files = dict.fromkeys(out_files) | |
try: | |
size = os.path.getsize(src) | |
fin = os.open(src, READ_FLAGS) | |
stat = os.fstat(fin) | |
for dst_file in out_files: | |
out_files[dst_file] = os.open(dst_file, WRITE_FLAGS, stat.st_mode) | |
for x in iter(lambda: os.read(fin, BUFFER_SIZE), ""): | |
workers = [] | |
for dst_file in out_files: | |
workers.append(multiprocessing.Process(target=copy_worker, args=(x,out_files[dst_file],))) | |
for worker in workers: | |
worker.start() | |
for worker in workers: | |
worker.join() | |
finally: | |
try: os.close(fin) | |
except: pass | |
try: | |
for key in out_files: | |
os.close(out_files[key]) | |
except: pass | |
def copy_worker(stream, fout): | |
os.write(fout, stream) | |
return | |
if __name__ == '__main__': | |
initTime = time.time() | |
copy_file(SRC_FILE, [DST_FILE], only_new_file=False) | |
print time.time() - initTime | |
time.sleep(5) | |
from PyQt5.QtWidgets import QApplication, QWidget | |
app = QApplication(sys.argv) | |
w = QWidget() | |
w.resize(250, 150) | |
w.move(300, 300) | |
w.setWindowTitle('Simple') | |
w.show() | |
initTime = time.time() | |
copy_file(SRC_FILE, [DST_FILE], only_new_file=False) | |
print time.time() - initTime | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment