2014-06-20 78 views
2

我有以下python代碼 - 創建三個線程。但是,在第一個線程創建後,控件似乎不會回來。輸出結果是一個「。」,而不是三個預期的「。」。工作人員功能有問題嗎? Msglyr是一個內部消息庫。Python無法啓動多個線程

#!/usr/bin/python 

import thread 
import time 
import Msglyr 

# Worker thread 
def worker(num): 
    testchannel = Msglyr.Channel("TEST-ASYNC-ROUTER-DEALER-CHANNEL", Msglyr.mlr.ASYNC_ROUTER_DEALER, Msglyr.mlr.ASYNC_DEALER_WORKER) 
    testchannel.setEndPoint(Msglyr.mlr.ASYNC_DEALER_WORKER, "inproc://test-pt") 
    testchannel.setEndPoint(Msglyr.mlr.ASYNC_DEALER_CLIENT, "tcp://127.0.0.1:5558") 

    while True : 
     msg = Msglyr.Message() 
     testchannel.receive(msg) 
     if msg.getnumParts() > 0: 
      msg.setPart(msg.getnumParts() - 1, "Worker : " + str(num)) 
     testchannel.send(msg) 

# Creating three threads 
try: 
    thread.start_new_thread(worker, (1,)) 
    print '.' 
    time.sleep(2) 

    thread.start_new_thread(worker, (2,)) 
    print '.' 
    time.sleep(2) 

    thread.start_new_thread(worker, (3,)) 
    print '.' 
    time.sleep(2) 
except: 
    print "Error: unable to start thread" 

print 'Started threads' 
while 1: 
    pass 

回答

1

我不確定這真的是一個答案(不是你所期望的),但我不能做更多。我把你的代碼,並與

# Worker thread 
def worker(num): 
    i = 1 
    while True : 
     print("Thread", num, i); 
     i += 1 
     time.sleep(3) 

一切順利的罰款代替工人,我可以看到郵件形式3線(1秒間隔...),我也看到了3個點。恐怕問題在於Msglyr