From 04f0aef3df92a7c26851cbc42d081634fc5943e0 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Sat, 30 Jun 2018 18:49:25 +0300 Subject: [PATCH] Threads fixed --- toxygen/middleware/callbacks.py | 10 ++-------- toxygen/middleware/threads.py | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/toxygen/middleware/callbacks.py b/toxygen/middleware/callbacks.py index 41a5717..513cdd2 100644 --- a/toxygen/middleware/callbacks.py +++ b/toxygen/middleware/callbacks.py @@ -198,10 +198,7 @@ def file_recv_chunk(file_transfer_handler): """ def wrapped(tox, friend_number, file_number, position, chunk, length, user_data): chunk = chunk[:length] if length else None - if length: - execute(file_transfer_handler.incoming_chunk, friend_number, file_number, position, chunk) - else: - invoke_in_main_thread(file_transfer_handler.incoming_chunk, friend_number, file_number, position, chunk) + execute(file_transfer_handler.incoming_chunk, friend_number, file_number, position, chunk) return wrapped @@ -211,10 +208,7 @@ def file_chunk_request(file_transfer_handler): Outgoing chunk """ def wrapped(tox, friend_number, file_number, position, size, user_data): - if size: - execute(file_transfer_handler.outgoing_chunk, friend_number, file_number, position, size) - else: - invoke_in_main_thread(file_transfer_handler.outgoing_chunk, friend_number, file_number, position, size) + invoke_in_main_thread(file_transfer_handler.outgoing_chunk, friend_number, file_number, position, size) return wrapped diff --git a/toxygen/middleware/threads.py b/toxygen/middleware/threads.py index 2e432b6..192f723 100644 --- a/toxygen/middleware/threads.py +++ b/toxygen/middleware/threads.py @@ -3,8 +3,13 @@ import threading import queue from utils import util import time +from PyQt5 import QtCore +# ----------------------------------------------------------------------------------------------------------------- +# Base threads +# ----------------------------------------------------------------------------------------------------------------- + class BaseThread(threading.Thread): def __init__(self): @@ -16,6 +21,21 @@ class BaseThread(threading.Thread): self.join() +class BaseQThread(QtCore.QThread): + + def __init__(self): + super().__init__() + self._stop_thread = False + + def stop_thread(self): + self._stop_thread = True + self.wait() + + +# ----------------------------------------------------------------------------------------------------------------- +# Toxcore threads +# ----------------------------------------------------------------------------------------------------------------- + class InitThread(BaseThread): def __init__(self, tox, plugin_loader, settings): @@ -53,7 +73,7 @@ class InitThread(BaseThread): time.sleep(5) -class ToxIterateThread(BaseThread): +class ToxIterateThread(BaseQThread): def __init__(self, tox): super().__init__() @@ -65,7 +85,7 @@ class ToxIterateThread(BaseThread): time.sleep(self._tox.iteration_interval() / 1000) -class ToxAVIterateThread(BaseThread): +class ToxAVIterateThread(BaseQThread): def __init__(self, toxav): super().__init__() @@ -77,6 +97,10 @@ class ToxAVIterateThread(BaseThread): time.sleep(self._toxav.iteration_interval() / 1000) +# ----------------------------------------------------------------------------------------------------------------- +# File transfers thread +# ----------------------------------------------------------------------------------------------------------------- + class FileTransfersThread(BaseThread): def __init__(self): @@ -115,6 +139,10 @@ def execute(func, *args, **kwargs): _thread.execute(func, *args, **kwargs) +# ----------------------------------------------------------------------------------------------------------------- +# Invoking in main thread +# ----------------------------------------------------------------------------------------------------------------- + class InvokeEvent(QtCore.QEvent): EVENT_TYPE = QtCore.QEvent.Type(QtCore.QEvent.registerEventType())