作者wandog (慢跑中毒)
看板Python
標題[問題] thread無法動作
時間Thu Dec 13 20:14:48 2012
當我把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)
推 sheauhei:將你的code照貼了,python 2.5.4可以跑 12/13 21:44
→ sheauhei:除了PLCcontrol() 因為你沒寫 我只是單純print msg 12/13 21:45
推 sheauhei:倒是將sleep(10)那行拿掉,main thread結束後, 12/13 21:49
→ sheauhei:workerThread(tPLC)會被強制終止 12/13 21:49
→ wandog:我是在UBUNTU SERVER EDITION下執行 不知道是不是OS的影響? 12/13 21:58
→ wandog:我有用類似一樓的方式 就是掛掉... 12/13 21:59
→ wandog:我當初加上time.sleep(10)的版本 在windows ce上ok..見鬼了 12/13 22:00
推 sheauhei:我的確是在Windows上跑的,OS不同就沒辦法了 囧" 12/13 22:07
→ wandog:看來只好work around了...orz 12/13 22:15
→ kdjf:PLCcontrol是? 12/14 09:30
→ kdjf:你的正常運作是什麼意思?我跑起來沒有問題啊 12/14 09:34
→ kdjf:印出Argspec, [], noargments, xxxx(我print的東西,sleep的秒 12/14 09:36
→ kdjf:數到的就結柬 12/14 09:36
→ wandog:是一個控制SERIAL port的含式 我把他換成一般print的動作 12/14 11:13
→ wandog:還是FAIL掉... 12/14 11:13
→ kdjf:你還是沒有講說你期得看到的是什麼啊XD 12/14 22:52
→ kdjf:另外,你的系統有更新嗎? 12/14 22:53