Skip to content

Commit

Permalink
Merge pull request #2 from Lafcadia/master
Browse files Browse the repository at this point in the history
Rewrote the Multiprocessing Part
  • Loading branch information
Lafcadia authored Jun 22, 2024
2 parents bdf62fc + 508e60b commit ca4571f
Showing 1 changed file with 36 additions and 23 deletions.
59 changes: 36 additions & 23 deletions pse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,57 @@
from plugin.engine.tinksp import Tinksp
from concurrent.futures import ThreadPoolExecutor, wait
from PySide6.QtWidgets import QMainWindow, QApplication, QMessageBox, QTreeWidgetItem
from PySide6.QtCore import QObject, QThread, Signal, Slot
# from PySide6 import QtCore
import sys
import multiprocessing as m
from multiprocessing import Process as P
from ui import Ui_MainWindow
import pyperclip

def get_result(keyword, q):
engine = [Spigot, Tinksp, Minebbs, Klpbbs]
results = []
with ThreadPoolExecutor(max_workers=6) as executor:
future_list = []
for e in engine:
future = executor.submit(e().search, keyword)
print(0)
future_list.append(future)
wait(future_list)
for future in future_list:
for result in future.result():
results.append(result)
print(1)
q.put(results)
class Worker(QObject):
_signal = Signal(list)
def __init__(self):
super().__init__()
print("000")

def work(self, keyword):
print(111)
engine = [Spigot, Tinksp, Minebbs, Klpbbs]
results = []
with ThreadPoolExecutor(max_workers=6) as executor:
future_list = []
for e in engine:
future = executor.submit(e().search, keyword)
print(10)
future_list.append(future)
wait(future_list)
for future in future_list:
for result in future.result():
results.append(result)
print(1)
self._signal.emit(results)

class MainWindow(QMainWindow):
a = Signal(str)

def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.worker = Worker()
self.worker_thread = QThread()
self.worker._signal.connect(self.shower)
self.worker.moveToThread(self.worker_thread)
self.a.connect(self.worker.work)
self.ui.searchButton.clicked.connect(self.search)
self.ui.outputTreeWidget.itemDoubleClicked.connect(self.copyUrl)

self.worker_thread.start()

def search(self):
self.ui.outputTreeWidget.clear()
keyword = self.ui.searchLine.text()
q = m.Queue()
p = P(target=get_result, args=(keyword, q))
p.start(); p.join(); results = q.get()
self.ui.outputTreeWidget.clear()
self.a.emit(keyword)

def shower(self, results):
for result in results:
num = results.index(result)
title = QTreeWidgetItem(self.ui.outputTreeWidget, [result.title, result.url, result.summary])
Expand All @@ -51,7 +65,6 @@ def copyUrl(self, item):
QMessageBox.information(self, "Copied!", "已将该插件链接复制到剪贴板。")

if __name__ == "__main__":
m.freeze_support()
app = QApplication(sys.argv)
window = MainWindow()
window.show()
Expand Down

0 comments on commit ca4571f

Please sign in to comment.