Created
August 26, 2015 06:15
-
-
Save chrisheckey/b543f340c54c2e80319a to your computer and use it in GitHub Desktop.
Thread pool mockup
This file contains hidden or 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 Queue | |
from PySide.QtCore import * | |
from path import Path | |
class FileCopyWorker(QObject): | |
progress = Signal() | |
done = Signal() | |
def __init__(self, parent=None): | |
super(FileCopyWorker, self).__init__(parent) | |
def copy_with_progress(self, source_path, target_path): | |
source_path = Path(source_path) | |
target_path = Path(target_path) | |
size = source_path.stat().st_size | |
copied = 0 | |
source = source_path.open('rb') | |
self.safe_make_folder(target_path.parent) | |
target = target_path.open('wb') | |
try: | |
while True: | |
chunk = source.read(1024) | |
if not chunk: | |
break | |
target.write(chunk) | |
copied += len(chunk) | |
self.progress.emit(copied * 100 / size) | |
finally: | |
source.close() | |
target.close() | |
@staticmethod | |
def safe_make_folder(path): | |
try: | |
if not path.exists(): | |
path.makedirs() | |
except WindowsError: | |
pass | |
class FileCopyJob(object): | |
def __init__(self, source, target, progress_bar): | |
self.source = source | |
self.target = target | |
self.progress_bar = progress_bar | |
class FileCopyThread(QThread): | |
done = Signal() | |
def __init__(self, queue, parent=None): | |
super(FileCopyThread, self).__init__(parent) | |
self.queue = queue | |
self.stop = False | |
def run(self): | |
worker = FileCopyWorker() | |
while True: | |
job = self.queue.get() | |
worker.progress.connect(job.progress_bar.setValue) | |
worker.copy_with_progress(job.source, job.target) | |
worker.progress.disconnect(job.progress_bar.setValue) | |
if self.stop: | |
break | |
if self.queue.empty(): | |
break | |
self.done.emit() | |
class FileCopyThreadPool(QObject): | |
done = Signal() | |
def __init__(self, parent=None): | |
super(FileCopyThreadPool, self).__init__(parent) | |
self.queue = Queue.Queue() | |
self.threads = [FileCopyThread(self.queue) for _ in range(10)] | |
for thread in self.threads: | |
thread.done.connect(self.done) | |
def add_job(self, source, target, progress_bar): | |
self.queue.put(FileCopyJob(source, target, progress_bar)) | |
def stop_all(self): | |
for thread in self.threads: | |
thread.stop = True | |
thread.wait() | |
def start_all(self): | |
for thread in self.threads: | |
thread.stop = False | |
thread.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment