Python 如何将json对象或字符串传递给进程

Python 如何将json对象或字符串传递给进程,python,json,Python,Json,我有一个程序,它根据接收到的数据执行其他python脚本。 它接收的数据是json格式的,对执行的脚本很有帮助 这就是为什么我希望这些脚本以某种方式接收json。我想使用Popen使用subprocess模块来实现这一点,但我认为它不起作用,因为我必须发送一个转义字符串或json对象(在json.loads()方法之后)。 我也可以将json写入文件并读取它,但这似乎是一个糟糕的选择 那么我该如何优雅地实现这一点呢?如果一次只有一个子进程,并且父进程将等待子进程完成,那么您可以使用。例如: #

我有一个程序,它根据接收到的数据执行其他python脚本。 它接收的数据是json格式的,对执行的脚本很有帮助

这就是为什么我希望这些脚本以某种方式接收json。我想使用
Popen
使用
subprocess
模块来实现这一点,但我认为它不起作用,因为我必须发送一个转义字符串或json对象(在json.loads()方法之后)。 我也可以将json写入文件并读取它,但这似乎是一个糟糕的选择


那么我该如何优雅地实现这一点呢?

如果一次只有一个子进程,并且父进程将等待子进程完成,那么您可以使用。例如:

# Create child with a pipe to STDIN.
child = subprocess.Popen(..., stdin=subprocess.PIPE)

# Send JSON to child's STDIN.
# - WARNING: This will block the parent from doing anything else until the
#   child process finishes
child.communicate(json_str)
然后,在子流程(如果是python)中,您可以使用以下命令读取它:

# Read JSON from STDIN.
json_str = sys.stdin.read()

或者,如果希望父进程可以多次写入多个子进程,则在父进程中:

# Create child with a pipe to STDIN.
child = subprocess.Popen(..., stdin=subprocess.PIPE)

# Serialize and send JSON as a single line (the default is no indentation).
json.dump(data, child.stdin)
child.stdin.write('\n')

# If you will not write any more to the child.
child.stdin.close()
然后,在儿童中,您可以根据需要阅读每一行:

# Read a line, process it, and do it again.
for json_line in sys.stdin:
    data = json.loads(json_line)
    # Handle received data.

如果一次只有一个子进程,并且父进程将等待子进程完成,则可以使用。例如:

# Create child with a pipe to STDIN.
child = subprocess.Popen(..., stdin=subprocess.PIPE)

# Send JSON to child's STDIN.
# - WARNING: This will block the parent from doing anything else until the
#   child process finishes
child.communicate(json_str)
然后,在子流程(如果是python)中,您可以使用以下命令读取它:

# Read JSON from STDIN.
json_str = sys.stdin.read()

或者,如果希望父进程可以多次写入多个子进程,则在父进程中:

# Create child with a pipe to STDIN.
child = subprocess.Popen(..., stdin=subprocess.PIPE)

# Serialize and send JSON as a single line (the default is no indentation).
json.dump(data, child.stdin)
child.stdin.write('\n')

# If you will not write any more to the child.
child.stdin.close()
然后,在儿童中,您可以根据需要阅读每一行:

# Read a line, process it, and do it again.
for json_line in sys.stdin:
    data = json.loads(json_line)
    # Handle received data.

如果一次只有一个子进程,并且父进程将等待子进程完成,则可以使用。例如:

# Create child with a pipe to STDIN.
child = subprocess.Popen(..., stdin=subprocess.PIPE)

# Send JSON to child's STDIN.
# - WARNING: This will block the parent from doing anything else until the
#   child process finishes
child.communicate(json_str)
然后,在子流程(如果是python)中,您可以使用以下命令读取它:

# Read JSON from STDIN.
json_str = sys.stdin.read()

或者,如果希望父进程可以多次写入多个子进程,则在父进程中:

# Create child with a pipe to STDIN.
child = subprocess.Popen(..., stdin=subprocess.PIPE)

# Serialize and send JSON as a single line (the default is no indentation).
json.dump(data, child.stdin)
child.stdin.write('\n')

# If you will not write any more to the child.
child.stdin.close()
然后,在儿童中,您可以根据需要阅读每一行:

# Read a line, process it, and do it again.
for json_line in sys.stdin:
    data = json.loads(json_line)
    # Handle received data.

如果一次只有一个子进程,并且父进程将等待子进程完成,则可以使用。例如:

# Create child with a pipe to STDIN.
child = subprocess.Popen(..., stdin=subprocess.PIPE)

# Send JSON to child's STDIN.
# - WARNING: This will block the parent from doing anything else until the
#   child process finishes
child.communicate(json_str)
然后,在子流程(如果是python)中,您可以使用以下命令读取它:

# Read JSON from STDIN.
json_str = sys.stdin.read()

或者,如果希望父进程可以多次写入多个子进程,则在父进程中:

# Create child with a pipe to STDIN.
child = subprocess.Popen(..., stdin=subprocess.PIPE)

# Serialize and send JSON as a single line (the default is no indentation).
json.dump(data, child.stdin)
child.stdin.write('\n')

# If you will not write any more to the child.
child.stdin.close()
然后,在儿童中,您可以根据需要阅读每一行:

# Read a line, process it, and do it again.
for json_line in sys.stdin:
    data = json.loads(json_line)
    # Handle received data.


退房它提供了一个简单的接口,可以使用计算机上的端口在任何进程体系结构之间发送序列化数据。您只需通过STDIN发送序列化JSON。使用
Subprocess.Popen()
?@Shookie,父进程是否会同时运行多个子进程,或者一次只能有一个子进程?@cpburnz-目前只有一个子进程。请查看。它提供了一个简单的接口,可以使用计算机上的端口在任何进程体系结构之间发送序列化数据。您只需通过STDIN发送序列化JSON。使用
Subprocess.Popen()
?@Shookie,父进程是否会同时运行多个子进程,或者一次只能有一个子进程?@cpburnz-目前只有一个子进程。请查看。它提供了一个简单的接口,可以使用计算机上的端口在任何进程体系结构之间发送序列化数据。您只需通过STDIN发送序列化JSON。使用
Subprocess.Popen()
?@Shookie,父进程是否会同时运行多个子进程,或者一次只能有一个子进程?@cpburnz-目前只有一个子进程。请查看。它提供了一个简单的接口,可以使用计算机上的端口在任何进程体系结构之间发送序列化数据。您只需通过STDIN发送序列化JSON。使用
Subprocess.Popen()
?@Shookie,父进程是否会同时运行多个子进程,或者一次只能有一个子进程?@cpburnz-目前只有一个子进程。这会不会有问题,因为json被转义了,所以它会一直读到第一行新代码?@Shookie,这取决于。如果您执行整个
.read()
(等待STDIN关闭),JSON可以有任意多的新行。如果您希望JSON只在一行上,那么必须确保JSON序列化时没有缩进,在这种情况下,您需要使用
.readline()
。我已经按照您所说的做了,但是我似乎从
子进程中抛出了
childe_异常。可能是因为我的命令是以
pythona/script.py
的形式出现的吗?另外,在你给出的第二个例子中,它不是
child.stdin
而不是
sys.stdin
?@Shookie你能把整个例外放在你的问题中,或者看看你是否正在经历这样的情况吗?是的,它应该是
child.stdin
而不是
sys.stdin
。这会不会有问题,因为json是转义的,所以它会一直读到第一行新行?@Shookie,这取决于。如果您执行整个
.read()
(等待STDIN关闭),JSON可以有任意多的新行。如果您希望JSON只在一行上,那么必须确保JSON序列化时没有缩进,在这种情况下,您需要使用
.readline()
。我已经按照您所说的做了,但是我似乎从
子进程中抛出了
childe_异常。可能是因为我的命令是以
pythona/script.py
的形式出现的吗?另外,在你给出的第二个例子中,它不是
child.stdin
而不是
sys.stdin
?@Shookie你能把整个例外放在你的问题中,或者看看你是否正在经历这样的情况吗?是的,它应该是
child.stdin
而不是
sys.stdin
。这会不会有问题,因为json是转义的,所以它会一直读到第一行新行?@Shookie,这取决于。如果您执行整个
.read()
(等待STDIN关闭),JSON可以有任意多的新行。如果希望JSON只在一行上,那么必须确保JS