2013-01-11 28 views
1

我的代码是:线程不参考任何工作显然将代码

类客户端(DirectObject,对象):

''' 
Clientclass. This class processes the keys which the user presses to the host and gets the data 
from the host, which are to be set in a dummy version of the model 
''' 

seed= "" 
id = None 

def __init__(self, ip): 
    ''' 
    This is the constructor. It creates the ShowBase an starts both the process which processes the keys and the process 
    which takes in data from the host 
    param: ip ip is a float which contains the ip adress of the host 

    ''' 
    logging.getLogger(__name__).info("Clientobjekt erstellt") 
    self.makw = mouseAndKeyWrapper.MouseAndKeyWrapper() 
    time.sleep(5)  
    from modules.logic import game 
    self.cprocess = threading.Thread(target = Client.workAsClient, args = (ip,self.makw)) 
    self.cprocess.start() 
    time.sleep(5) 
    while True: 
     if Client.seed != "": 
      break    
    game.seed = Client.seed 
    game.initGame() 
    game.initGameInstance()  
    game.start()  

    self.workShowBase() 
    game.myShowBase.run() 



def workShowBase(self): 

    ''' 
    workShowBase defines how to change values in keydict when keys are pressed and 
    starts ShowBase process 
    ''' 
    from modules.logic import game 
    logging.getLogger(__name__).info("Showbase is working") 
    game.myShowBase.accept("w", self.makw.setKeys, ["w",1]) 
    game.myShowBase.accept("a", self.makw.setKeys, ["a",1]) 
    game.myShowBase.accept("s", self.makw.setKeys, ["s",1]) 
    game.myShowBase.accept("d", self.makw.setKeys, ["d",1]) 
    game.myShowBase.accept("space", self.makw.setKeys, ["space",1]) 
    game.myShowBase.accept("w-up", self.makw.setKeys, ["w",0]) 
    game.myShowBase.accept("a-up", self.makw.setKeys, ["a",0]) 
    game.myShowBase.accept("s-up", self.makw.setKeys, ["s",0]) 
    game.myShowBase.accept("d-up", self.makw.setKeys, ["d",0]) 
    game.myShowBase.accept("space-up", self.makw.setKeys, ["space",0]) 
    #game.myShowBase.accept("mouse1",self.makw.setKeys,["mouse",1]) 
    #game.myShowBase.accept("mouse1-up",self.makw.setKeys,["mouse",0]) 





@staticmethod 
def workAsClient(ip, makw): 
    ''' 
    This method contains the client thread, that is, the thread which sends data to the host and 
    receives data from it - for this reason it has to be static 
    param: ip ip is a float which contains the ip adress of the host 
    ''' 
    logging.getLogger(__name__).info("Clientendlosschleife gestartet") 
    from modules.logic import game 
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    s.connect((ip, 50001)) 
    seedSend = s.recv(4096) 
    Client.seed =loads(seedSend) 
    ID = s.recv(4096) 
    Client.id = loads(ID)  
    makw.id = id 
    keyThread = threading.Thread(target =Client.sendData, args = (makw,s)) 
    print("threadmade") 
    keyThread.start() 
    print("started") 

@staticmethod  
def sendData(makw, sock): 
    print("method")  
    while True: 
     print("loop") 
     dictToSend = dumps(makw,2) 
     print("dumped") 
     sock.sendall(dictToSend) 
     print("sent") 

这确实做工精细。

但是,如果我在构造函数中省略了“time.sleep(5)”,那么调用sendData的线程永远不会被创建,更不用说启动了。 这怎么可能?技术上Showbase不应该干涉,在另一个过程中! 我可以解决这个问题吗?

回答

1

您应该避免使用全部活动等待,这可能会消耗所有资源,从而导致其他线程无法工作。

所以你可以更好的尝试:

while True: 
     if Client.seed != "": 
      break 
     time.sleep(10) 

事实上你的辅助线程启动,但如果所有的处理是由它的无限循环的主线程消耗它永远不会有一个工作的机会,此后再也没有提供种子主线程正在等待=>死锁

当您使用睡眠(5)时,您有机会调度次要线程并产生种子;那么当主线程重新安排后,它会发现种子,继续工作,一切都按预期进行。

但是你的解决方法是非常脆弱的,因为你不能保证在主线程的睡眠期间另一个将被安排。

它现在适用于您的开发环境,但可能会在以后的生产环境中稍后中断。

+0

我明白了......尽管你的变化似乎有效。 – user1862770

+0

看起来这是“但是这个解决方法......”没有被正确解释;):“这个”是你原来的解决方法;我已经编辑了我的答案,以便说清楚。我的代码示例是正确的方法,而不是解决方法:) – Pragmateek

+0

嗯......事情是,我做了20%的案例。有了你的解决方案,80%的案例都能解决问题,但仍然有20%的案例和以前一样,我不知道为什么。你有吗? – user1862770