为什么没有`stdin.read()`读取整个缓冲区?

时间:2014-08-25 10:16:20

标签: python json

我已获得以下代码:

def get_input(self):
    """
    Reads command from stdin, returns its JSON form
    """
    json_string = sys.stdin.read()
    print("json string is: "+json_string)
    json_data =json.loads(json_string)
    return json_data

def accept_commands(self):
    while True:
        json_data = self.get_input()
        command = self.command_analyzer.is_command(json_data) # Check wether the command exists. Return it if it does
        #The command exists
        if command is not None:
            #The addon is not currently active
            if analyzer.intent(json_data) not in self.active_addons:
                self.activate_addon(command,json_data)

            #The addon is active and so we need to send the data to the subprocess
            else:
                self.communicate_with_addon(command,json_data,json_string)

它读取从另一个进程发送给它的json字符串。从stdin读取json。 出于某种原因,我得到以下输出:

json string is: <Some json here>
json string is: 
Traceback (most recent call last):
  File "/Users/Matan/Documents/workspace/ProjectSH/addonmanager/addon_manager.py", line 63, in <module>
    manager.accept_commands()
  File "/Users/Matan/Documents/workspace/ProjectSH/addonmanager/addon_manager.py", line 49, in accept_commands
    json_data = self.get_input()
  File "/Users/Matan/Documents/workspace/ProjectSH/addonmanager/addon_manager.py", line 42, in get_input
    json_data =json.loads(json_string)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 383, in raw_decode
    raise ValueError("No JSON object could be decoded")

json从以下发送:

class MessageReceiver:

    def __init__(self):
        '''
        Connect to the AMQP broker and starts listening for messages.
        Creates the a Popen object to pass command info to the addon_manager script (which
        is in charge of managing scripts)
        '''
        addon_manager_path = configuration.addon_manager_path()
        addon_manager_path = os.path.join(addon_manager_path,'addon_manager.py')
        execute = "python " + addon_manager_path
        self.addon_manager = subprocess.Popen(execute, stdin=subprocess.PIPE, shell=True)


        self.component_name= configuration.get_attribute("name")

        if len(sys.argv)>1:
            host_ip = sys.argv[1]
        else:
            host_ip = 'localhost'

        #Start a connection to the AMQP server
        self.connection = pika.BlockingConnection(pika.ConnectionParameters(host=host_ip))

        #Create a channel to the server
        self.channel = self.connection.channel()

        self.channel.queue_declare(queue="kitchen")

        #callback method to be called when data is received
        #It sends the data that is received by the client to the addon_manager
        def data_received(ch, method, properties, body):
            ##TODO: Might want to add some checks. Is body a JSON? etc.
            print("writing data to addon_manager")
            self.addon_manager.communicate(body)


        self.channel.basic_consume(data_received,queue='kitchen',no_ack=True)


        self.channel.start_consuming()

这里有什么问题?

2 个答案:

答案 0 :(得分:2)

默认情况下,stdin.read() 阻止,直到读取整个缓冲区。如果您只能解码一个JSON对象,那么就是在stdin被另一个进程关闭之前发送的所有内容。

如果需要流式传输多个JSON块,则应该

a)不要关闭写作过程中的流,并且 b)不要在Python中进行阻塞读取。

读取数据块或行;有关读取以行分隔的JSON对象或使用多行的JSON对象的技术,请参阅Loading and parsing a JSON file with multiple JSON objects in PythonHow do I use the 'json' module to read in one JSON object at a time?

你可以适应产生发电机功能;你循环遍历它以一次产生一个JSON对象,阻塞它们以等待下一个JSON对象:

def get_input(self):
    for line in sys.stdin:
        yield json.loads(line)

def accept_commands(self):
    for json_data in self.get_input():
        # do something with `json_data`

您正在使用Popen.communicate()写入管道。写入后,关闭管道,之后等待进程终止。

如果您希望管道保持打开状态,请不要使用Popen.communicate,而是直接写入Popen.stdin pipe

答案 1 :(得分:0)

好像你要两次调用 get_input ,而缓冲区中没有任何内容。在尝试解析JSON之前,您应该检查是否有某些内容:

def get_input(self):
    """
    Reads command from stdin, returns its JSON form
    """
    json_string = sys.stdin.read()
    if json_string:
        print("json string is: "+json_string)
        json_data =json.loads(json_string)
        return json_data
    else:
        return None