【发布时间】:2011-12-27 08:56:06
【问题描述】:
我正在编写一个脚本,用于通过 JSON/XML api 从图像板批量下载图像。以前,它纯粹是 CLI,但最近我一直在尝试在 PyQt 中构建 UI,取得了巨大成功,但有一个问题:线程阻塞问题,在我的脚本中实际调用工作线程时 GUI 无响应。所以,我试图从 threading.Thread 切换到 QThread,以使其更易于管理(通过发出 threadFinished SIGNAL 来更新我的 GUI),但我似乎无法正确设置它。每当我运行脚本时,线程都会过早死亡。我在 Windows 上运行,在 Python 2.7.2 上使用 PyQt4。
经过更多研究,我认为问题出在线程退出,并创建一个新线程,并从队列中传递一个新元组。我可以在网上找到的所有结果都表明应用程序没有完全退出。
Exception KeyError: KeyError(1188,) in <module 'threading' from 'C:\Python27\lib\threading.pyc'> ignored
QObject::killTimers: timers cannot be stopped from another thread
这是我收到的输出。
有问题的直接代码:
md5_queue 是由 Url_Download() 填充的 md5sum/文件名的空字典的队列 queue 是文件名/url 的队列元组
在 crawler.py 中:
num_conn = int(max_threads)
threads = []
for download in range(num_conn):
t = Url_Download(queue, md5_queue)
t.start()
threads.append(t)
来自functions.py(名字不好,我知道)Url_Download() 类:
class Url_Download(QThread):
file_finished = pyqtSignal(QString, int, name="fileFinished")
def __init__(self, dl_queue, md5_queue):
self.dl_queue = dl_queue
self.md5_queue = md5_queue
QThread.__init__(self)
def run(self):
while 1:
try:
count = 0
file_url, file_path, md5 = self.dl_queue.get_nowait()
file_extension = str(file_url)[-4:]
file_name = md5 + file_extension
while count < 3:
count +=1
fetch_url(file_url, file_path, md5)
if md5 == hash_sum(file_path):
self.md5_queue.put_nowait((md5, file_name))
self.file_finished.emit("Test!", 10)
break
if count > 3:
print 'File failed to download, {} might be corrupt.'.format(file_name)
qsize = self.dl_queue.qsize()
if qsize > 0:
print 'Count Remaining: ', qsize
except Queue.Empty:
raise SystemExit
except:
traceback.print_exc(file=sys.stderr)
sys.stderr.flush()
从 GUI.py 中,插槽连接:
self.connect(self, SIGNAL("fileFinished(QString, int)"), self.handle_test, Qt.QueuedConnection)
代码的 Git(测试分支):https://github.com/CirnoIsTheStrongest/BiriCrawler/tree/testing
请注意,这是我第一次尝试编写任何代码。如果这是个问题,请告诉我
【问题讨论】:
-
我无法解决您面临的问题,但
connect行是错误的。您正在连接QMainWindow的fileFinished信号,该信号不存在。
标签: python windows multithreading pyqt