如何存储和访问许多词典

时间:2019-03-24 12:31:53

标签: python python-3.x multithreading sockets

我有一个套接字连接,例如客户端和服务器。所以我将数据从客户端发送到服务器,并在服务器端,我将其作为字典存储为键和值对。但是我不确定如何堆叠。 例如:我正在发送一条消息“动物”,并将其存储为my_dict = {a:b},其中a是消息,b是与之关联的IP地址。所以我想要的是,如果我以“人类”的身份发送消息。我想存储消息和IP地址。但是当我打印my_dict时,它总是给出我发送的最后一条消息。我实际上想打印存储的整个词典。我想打印'animal':ip和'human':ip。

我想访问该词典,例如以动物为主题,并以ip作为通信的连接ip地址。 (公共订阅)

服务器端:

    def bd_recv(host,port):
        my_dict=dict()
        #listening bd for other devices and controllers
        sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
        address=host,port
        sock.bind(address)
        msg,client=sock.recvfrom(4089)
        a=msg.decode()
        b=client[0]
        my_dict={a:b}

        for key,value in my_dict.items():
            print (key)
    def pub-sub():
       context = zmq.Context()
       sub=context.socket(zmq.SUB)  # Note.
       #for the animal topic here i will refer to dictionary. if this animal topic is there i will take that key and value(ip) and append that to the variable ip here.
       sub.setsockopt_string(zmq.SUBSCRIBE, 'animal:')  # Note.
       ip=# here comes the value for animal which is ip.
       sub.connect('tcp://ip:8000')
       for i in range(2):
       print('Received: %s' % sub.recv())

    if __name__=="__main__":
        t1 = threading.Thread(target=bd_recv, name='broadcast_receive', args=(bd_of_the_machine, 9289))
        threads=threading.Thread(target=pub-sub)
        t1.start()
        threads.start()

2 个答案:

答案 0 :(得分:0)

每次收到消息时,您都将覆盖my_dict变量,因此将仅保存最后一个。尝试将该行更改为

my_dict[a] = b

同样,您的字典需要在函数体之外声明:

my_dict = {}

def bd_recv(host,port):        
    #listening bd for other devices and controllers
    sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    address=host,port
    sock.bind(address)
    msg,client=sock.recvfrom(4089)
    a=msg.decode()
    b=client[0]
    my_dict[a] = b

    for key,value in my_dict.items():
        print (key)

答案 1 :(得分:0)

在服务器端,您必须在bd_recv之外的范围内做出命令,例如在bd_recv中,您必须返回命令{a:b},您应该将其添加到该命令中

更新:

import socket
import threading
import time

total_dict = {}

def bd_recv(host, port):
    global total_dict
    while True:
        my_dict=dict()
        #listening bd for other devices and controllers
        sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
        address=host,port
        sock.bind(address)
        msg,client = sock.recvfrom(4089)
        a = msg.decode()
        b = client[0]
        my_dict = {a:b}
        total_dict.update({a:b})

        for key,value in my_dict.items():
            print (key)

def print_all_animals():
    while True:
        print(total_dict)
        time.sleep(1)

if __name__=="__main__":
    t1 = threading.Thread(
        target=bd_recv,
        name='broadcast_receive',
        args=("0.0.0.0", 9289)
    )
    t2 = threading.Thread(
        target=print_all_animals,
        name="print_all_animals",
        # args=()
    )
    t1.start()
    t2.start()

但是我不认为使用全局变量是一个好的解决方案,但是您没有提供更多的上下文或模型。在大多数情况下,使用某种锁定存储的队列会更好,更“确定”。这完全取决于您要如何在代码中访问此字典,而您没有提供它。