从不同功能访问时,字典返回不同的值

时间:2019-05-10 18:30:11

标签: python list dictionary

最小示例

将打印内容放入get_tuple_space函数内:

def get_tuple_space():
    global tuple_space
    print("VALUE INSIDE GETTER")
    print(tuple_space)
    return tuple_space

当我在server.py文件中调用tuple_space的函数时,就得到了预期的结果,即填充了一些元素的tuple_space。

while True:
        # Get the list sockets which are ready to be read through select
        readable,writable,exceptional = select.select(connected_list,[],[])

        for sock in readable:
            #New connection
            if sock == server_socket:
                connect()

            #Some incoming message from a client
            else:
                handle_incoming_msg(sock)
                print("Trying to get tuple space inside server")
                print(get_tuple_space())

但是,尝试按如下所示在我的client.py中调用此函数会给我和空字典:

import socket, select, string, sys
import server

#some code
#...
#...

while True:
        socket_list = [sys.stdin, client_socket]

        # Get the list of sockets which are readable
        rList, wList, error_list = select.select(socket_list , [], [])

        for sock in rList:
            #user entered a message
            msg=sys.stdin.readline()
            linda.blog_out(my_name, my_topic, msg, client_socket)

            #HERE
            print("trying to get tuple space inside client")
            print(server.get_tuple_space())

            #reads from topic
            messages = linda.blog_rd(my_topic)
            print messages

示例结束

更新

在使用该词典的函数中添加了“ global tuple_space”。另外,我尝试在服务器代码中调用get_tuple_space,它返回了正确的值。当我从另一个代码调用该函数时,似乎出现了问题。

更新结束

我在使用Python时可能遇到了一个非常初学者的问题。我正在尝试使用套接字和字典来编写一些代码,但奇怪的是我的字典返回值填充了一些函数,而空的却填充了另一个函数。我已经将他设置为全局角色,因此我希望它能返回我所有职能中所载的收益。

我相信我缺少有关python作用域的某些信息。似乎所有与套接字相关的东西都工作正常。

首先,我的字典在server.py中初始化如下

tuple_space={}

例如,此后,第一个函数将客户端连接到服务器套接字,并接收包含其名称,感兴趣的主题和所写消息的消息。然后,我的程序使用一些信息更新了de tuple_space字典。第二个函数做的事情非常相似,使用来自该客户端的有关该主题的新消息来更新tuple_space字典。

两者都返回我字典的最新版本,并且正在我的服务器代码中运行。

def connect():

    sockfd, addr = server_socket.accept()

    data =str(sockfd.recv(buffer))
    name, topic, msg = data.split("@")

    connected_list.append(sockfd)
    tuple_space[(name, topic)]=""

    #add name and address
    registered_names.append(name)

    tuple_space[(name,topic)] = msg


def handle_incoming_msg(sock):
    # Data from client
    data = sock.recv(buffer)

    name, topic, msg = data.split("@")

    tuple_space[(name, topic)] = msg

    print(tuple_space)

这两个函数都在我的server.py内部使用。第一个是新客户端连接并发送其名称,主题和消息时发送的,第二个是连接客户端发送某些内容时发送的消息:

def main():
    server_socket.bind((host, port))
    server_socket.listen(10) #listen atmost 10 connection at one time

    # Add server socket to the list of readable connections
    connected_list.append(server_socket)
    print("Servidor inicializado\n")

    while True:
        # Get the list sockets which are ready to be read through select
        readable,writable,exceptional = select.select(connected_list,[],[])

        for sock in readable:
            #New connection
            if sock == server_socket:
                connect()

            #Some incoming message from a client
            else:
                handle_incoming_msg(sock)

    server_socket.close()

在那之后,我想在我的client.py代码中接收存储在tuple_space中的最后一条消息。这是我在客户端循环中执行的操作:

messages_list = linda.blog_rd(my_topic)
            if len(messages_list)>0:
                print(messages_list)

blog_rd函数如下,在我的类“琳达”中定义

def blog_rd(self, topic):

        incoming_messages = []

        registered_names = get_registered_names()
        tuple_space = get_tuple_space()

        for i in range(len(registered_names)):
            name = registered_names[i]
            print(name)
            incoming_messages.append((name, tuple_space[(name,topic)]))

        return incoming_messages

本来是要获取注册名称并获取存储在tuple_space中的messagens的,然后调用get_tuple_space函数。

问题从这里开始:

def get_tuple_space():
    print(tuple_space)
    return tuple_space

当我调用此函数时,全局的tuple_space字典返回空。它也发生在registered_names列表中。我创建了关于字典的帖子,以简化我的解释,因为registered_names中的错误与tuple_space中的错误相同。

在我的项目中,此功能在另一个代码文件上调用,通过导入server.py来访问。但是令我困惑的是,即使在打印中,tuple_space也为空。

我期望它会像以前的两个函数一样打印所有存储的值。

我在做什么错了?

2 个答案:

答案 0 :(得分:1)

据我了解,tuple_space在文件中声明,并在一个或多个其他文件中使用。但是Python中的所有变量都包含在其模块的命名空间(创建该文件的文件)中。因此,如果在tuple_space中声明了server.py,则必须使用from server import tuple_space将其导入另一个文件。

答案 1 :(得分:0)

感谢所有尝试提供帮助的人。

正如我所说,我在举一个最小示例时遇到了一些麻烦,因为在没有套接字的最小示例中,它可以满足您的建议,但在实际项目中却没有用。

我要做的是改变我表示tuple_space的方式。我将其设置为具有所需属性的Class,之前它已放入字典中。因此,server.py使用该对象并根据从服务器发送的消息来处理它。

不知道以前发生了什么,但是我很高兴以某种方式解决了这个问题。

谢谢!

相关问题