[問題] 關於QThread

看板Python作者 (R5大小姐-EX人品崩壞)時間6年前 (2017/11/25 23:33), 6年前編輯推噓0(003)
留言3則, 2人參與, 6年前最新討論串1/1
各位前輩好 最近在寫PyQt4的Qthread遇到了一點問題 情境為 按下某個按鈕時,會將一些url放入Queue1裡面 接著用4個thread 從Queue1將url取出分析出該頁面裡面的一些文章標題url放入List ----現在做到這裡分隔線---- 然後從List取出文章標題url再存入另一個Queue2 等按下另一個按鈕,再從Queue2從文章標題url多thread下載文章內容 thread class程式碼如下: class Worker(QtCore.QThread): def __init__(self, threadId, name, queue, parent=None, allPageNum=None): super().__init__(parent) self.threadId = threadId self.queue = queue self.name = name self.allPageNum = allPageNum def run(self): if "getArticle" in self.name: while(not self.queue.empty()): pageNum = self.queue.get() articleList = article.get_page_article(self.allPageNum,pageNum) #這段是我取得文章標題的程式,回傳一個有文章標題url的List print(articleList) time.sleep 按鈕行為部分: threadList = [] self.pageQueue.queue.clear() ... #將範圍url放入queue for i in range(start,end+1): self.pageQueue.put(i) #創造4個thread for j in range(4): pageThread = Worker(i, "getArticle", self.pageQueue, allPageNum=self.allPageNum) threadList.append(pageThread) for thread in threadList: thread.start() 第一個問題是 一開始會跑出 QThread: Destroyed while thread is still running 然後程式就掛掉了 第二個問題是 在做判斷是否全部抓完的部分會有 while(not self.queue.empty()): AttributeError: 'tuple' object has no attribute 'queue' 的問題 然後沒辦法把全部的url抓出來 求解QQ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 59.120.251.134 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1511624032.A.BB8.html

11/25 23:39, 6年前 , 1F
對了 環境是python3
11/25 23:39, 1F

11/26 00:04, 6年前 , 2F
給的資訊太少, 看起來是你的 thread 放在錯誤的 scope
11/26 00:04, 2F

11/26 00:05, 6年前 , 3F
至少要有你的按鈕行為整個函式宣告, 而不是只有內容
11/26 00:05, 3F
按鈕行為是一個抓取使用者兩個輸入的頁數first final 變成範圍 self.connect(self.sendPageButton, QtCore.SIGNAL('clicked()'), self.parse_pages) def parse_pages(self): first , final #略 起始頁數&終止頁數 threadList = [] self.pageQueue.queue.clear() for i in range(first,final+1): self.pageQueue.put(i) for j in range(4): pageThread = Worker(i, "getArticle", self.pageQueue, allPageNum=self.allPageNum) threadList.append(pageThread) for thread in threadList: thread.start() 這是那顆按鈕的行為 而article.get_page_article()回傳值為一個帶有許多標題字串 的List 感謝 ※ 編輯: neil987 (219.68.18.225), 11/26/2017 02:51:31
文章代碼(AID): #1Q6OrWku (Python)