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 在技术上不应该干涉,在另一个过程中!我可以解决这个问题吗?

4

1 回答 1

1

您应该避免使用可能会消耗所有资源的完全活动等待,而让其他线程没有机会工作。

所以你最好试试:

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

实际上,您的辅助线程已启动,但是如果您的主线程在其无限循环中消耗了所有处理,它将永远没有机会工作,那么永远不会提供主线程正在等待的种子 =>死锁

当您使用 sleep(5) 时,您有机会安排辅助线程并产生种子;然后当后者重新安排主线程时,它会找到种子,继续工作,一切都按预期进行。

但是您的解决方法非常脆弱,因为您无法保证在主线程睡眠期间会安排另一个线程。

它现在可以在您的开发环境中运行,但稍后可能会在另一个环境中的生产中中断。

于 2013-01-11T17:01:58.990 回答