Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 线程套接字函数弄乱了局部变量_Python_Multithreading_Sockets - Fatal编程技术网

Python 线程套接字函数弄乱了局部变量

Python 线程套接字函数弄乱了局部变量,python,multithreading,sockets,Python,Multithreading,Sockets,我正在用Python制作一个小型测试服务器,使用TCP。插座堵塞,但我认为插座与此问题无关。我知道目前的解决方案有点难看和混乱,但它是经过大量的测试和调整,看看什么工作和什么不工作 该函数在类内的每个客户机的线程中运行 每个客户机(一个类)都有一个名称和一些其他先前设置的不相关的内容 问题是,每当连接两个或多个客户端时,客户端变量都设置为最后添加的客户端,在本例中为Computer2。您还可以看到索引变量从未受到影响 代码如下: def recieveDataFromClient(self, s

我正在用Python制作一个小型测试服务器,使用TCP。插座堵塞,但我认为插座与此问题无关。我知道目前的解决方案有点难看和混乱,但它是经过大量的测试和调整,看看什么工作和什么不工作

该函数在类内的每个客户机的线程中运行

每个客户机(一个类)都有一个名称和一些其他先前设置的不相关的内容

问题是,每当连接两个或多个客户端时,客户端变量都设置为最后添加的客户端,在本例中为Computer2。您还可以看到索引变量从未受到影响

代码如下:

def recieveDataFromClient(self, sock, index):
    while True:
        client = self.clients[index]
        recvStr = sock.recv(1024).decode()
        if not recvStr:
            break
        if client.name:
            print("got something from "+client.name+" on "+str(sock))
            print("Clients:")
            for client in self.clients:
                print(client.name)
            print("this client is: "+str(index)+" - "+self.clients[index].name+" aka "+client.name)
以下是从Computer1发送时的输出:

got something from Computer1 on <socket.socket object, fd=1044, family=2, type=1, proto=0>
Clients:
Computer1
Computer2
this client is: 0 - Computer1 aka Computer2
从计算机1上获取了一些信息
客户:
计算机1
计算机2
此客户端是:0-Computer1又名Computer2
以下是从Computer2发送的输出:

got something from Computer2 on <socket.socket object, fd=1100, family=2, type=1, proto=0>
Clients:
Computer1
Computer2
this client is: 1 - Computer2 aka Computer2
从计算机2上获取了一些信息
客户:
计算机1
计算机2
此客户端是:1-Computer2又名Computer2

您意外地覆盖了
客户端变量:

  client = self.clients[index]  # You assign client here
    recvStr = sock.recv(1024).decode()
    if not recvStr:
        break
    if client.name:
        print("got something from "+client.name+" on "+str(sock))
        print("Clients:")
        for client in self.clients:  # You're stomping on it here
            print(client.name)
        # Now when you use client below, its whatever came last in the for loop.
        print("this client is: "+str(index)+" - "+self.clients[index].name+" aka "+client.name)

只需在for循环中使用不同的名称。

您无意中覆盖了
客户端变量:

  client = self.clients[index]  # You assign client here
    recvStr = sock.recv(1024).decode()
    if not recvStr:
        break
    if client.name:
        print("got something from "+client.name+" on "+str(sock))
        print("Clients:")
        for client in self.clients:  # You're stomping on it here
            print(client.name)
        # Now when you use client below, its whatever came last in the for loop.
        print("this client is: "+str(index)+" - "+self.clients[index].name+" aka "+client.name)

只需在for循环中使用不同的名称。

您无意中覆盖了
客户端变量:

  client = self.clients[index]  # You assign client here
    recvStr = sock.recv(1024).decode()
    if not recvStr:
        break
    if client.name:
        print("got something from "+client.name+" on "+str(sock))
        print("Clients:")
        for client in self.clients:  # You're stomping on it here
            print(client.name)
        # Now when you use client below, its whatever came last in the for loop.
        print("this client is: "+str(index)+" - "+self.clients[index].name+" aka "+client.name)

只需在for循环中使用不同的名称。

您无意中覆盖了
客户端变量:

  client = self.clients[index]  # You assign client here
    recvStr = sock.recv(1024).decode()
    if not recvStr:
        break
    if client.name:
        print("got something from "+client.name+" on "+str(sock))
        print("Clients:")
        for client in self.clients:  # You're stomping on it here
            print(client.name)
        # Now when you use client below, its whatever came last in the for loop.
        print("this client is: "+str(index)+" - "+self.clients[index].name+" aka "+client.name)

只需在for循环中使用其他名称。

缩进显示不正确。你能修复它,或者确认我们看到的是你真正拥有的吗?现在修复了,当粘贴缩进显示不正确时意外删除了一个选项卡。你能修复它,或者确认我们看到的是你真正拥有的吗?现在修复了,当粘贴缩进显示不正确时意外删除了一个选项卡。你能修复它,或者确认我们看到的是你真正拥有的吗?现在修复了,当粘贴缩进显示不正确时意外删除了一个选项卡。你能修复它,或者确认我们看到的是你真正拥有的吗?现在修复了,粘贴时意外删除了一个选项卡