[問題] thread無法動作

看板Python作者 (慢跑中毒)時間13年前 (2012/12/13 20:14), 編輯推噓3(3014)
留言17則, 3人參與, 最新討論串1/1
當我把function丟入queue中時 執行緒跑到 某行就停掉了 當我把main()最下方的time.sleep(10)拿掉時 執行續又開始正常執行 我實在搞不懂為什麼 下方是我的code def PLCexecute(): while 1: PLCcontrol() class OBJthread(threading.Thread): def __init__(self,queue): threading.Thread.__init__(self) self.queue=queue def run(self): func,arg=self.queue.get() if func!=None: print inspect.getargspec(func) print inspect.getargspec(func)[0] if len(inspect.getargspec(func)[0])!=0: print "argumeents needed" func(arg) else: print "no arguments"<---跑到這邊就停住 有顯示這個字串 func() self.queue.task_done() time.sleep(0.5) self.queue.empty() def main(): #below part is about the multi-thread q1=Queue.Queue() tPLC=OBJthread(q1) tPLC.setDaemon(True) tPLC.start() q1.put((PLCexecute,"hello")) time.sleep(10)<--comment掉就正常運作... main() 請教各位了 究竟為何拿掉就正常運作.... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.117.120.110 ※ 編輯: wandog 來自: 140.117.120.110 (12/13 20:15) ※ 編輯: wandog 來自: 140.117.120.110 (12/13 20:18) ※ 編輯: wandog 來自: 140.117.120.110 (12/13 20:18)

12/13 21:44, , 1F
將你的code照貼了,python 2.5.4可以跑
12/13 21:44, 1F

12/13 21:45, , 2F
除了PLCcontrol() 因為你沒寫 我只是單純print msg
12/13 21:45, 2F

12/13 21:49, , 3F
倒是將sleep(10)那行拿掉,main thread結束後,
12/13 21:49, 3F

12/13 21:49, , 4F
workerThread(tPLC)會被強制終止
12/13 21:49, 4F

12/13 21:58, , 5F
我是在UBUNTU SERVER EDITION下執行 不知道是不是OS的影響?
12/13 21:58, 5F

12/13 21:59, , 6F
我有用類似一樓的方式 就是掛掉...
12/13 21:59, 6F

12/13 22:00, , 7F
我當初加上time.sleep(10)的版本 在windows ce上ok..見鬼了
12/13 22:00, 7F

12/13 22:07, , 8F
我的確是在Windows上跑的,OS不同就沒辦法了 囧"
12/13 22:07, 8F

12/13 22:15, , 9F
看來只好work around了...orz
12/13 22:15, 9F

12/14 09:30, , 10F
PLCcontrol是?
12/14 09:30, 10F

12/14 09:34, , 11F
你的正常運作是什麼意思?我跑起來沒有問題啊
12/14 09:34, 11F

12/14 09:36, , 12F
印出Argspec, [], noargments, xxxx(我print的東西,sleep的秒
12/14 09:36, 12F

12/14 09:36, , 13F
數到的就結柬
12/14 09:36, 13F

12/14 11:13, , 14F
是一個控制SERIAL port的含式 我把他換成一般print的動作
12/14 11:13, 14F

12/14 11:13, , 15F
還是FAIL掉...
12/14 11:13, 15F

12/14 22:52, , 16F
你還是沒有講說你期得看到的是什麼啊XD
12/14 22:52, 16F

12/14 22:53, , 17F
另外,你的系統有更新嗎?
12/14 22:53, 17F
文章代碼(AID): #1GoSQx48 (Python)
文章代碼(AID): #1GoSQx48 (Python)