作者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)
1F:推 sheauhei:将你的code照贴了,python 2.5.4可以跑 12/13 21:44
2F:→ sheauhei:除了PLCcontrol() 因为你没写 我只是单纯print msg 12/13 21:45
3F:推 sheauhei:倒是将sleep(10)那行拿掉,main thread结束後, 12/13 21:49
4F:→ sheauhei:workerThread(tPLC)会被强制终止 12/13 21:49
5F:→ wandog:我是在UBUNTU SERVER EDITION下执行 不知道是不是OS的影响? 12/13 21:58
6F:→ wandog:我有用类似一楼的方式 就是挂掉... 12/13 21:59
7F:→ wandog:我当初加上time.sleep(10)的版本 在windows ce上ok..见鬼了 12/13 22:00
8F:推 sheauhei:我的确是在Windows上跑的,OS不同就没办法了 囧" 12/13 22:07
9F:→ wandog:看来只好work around了...orz 12/13 22:15
10F:→ kdjf:PLCcontrol是? 12/14 09:30
11F:→ kdjf:你的正常运作是什麽意思?我跑起来没有问题啊 12/14 09:34
12F:→ kdjf:印出Argspec, [], noargments, xxxx(我print的东西,sleep的秒 12/14 09:36
13F:→ kdjf:数到的就结柬 12/14 09:36
14F:→ wandog:是一个控制SERIAL port的含式 我把他换成一般print的动作 12/14 11:13
15F:→ wandog:还是FAIL掉... 12/14 11:13
16F:→ kdjf:你还是没有讲说你期得看到的是什麽啊XD 12/14 22:52
17F:→ kdjf:另外,你的系统有更新吗? 12/14 22:53