我正在尝试学习如何使用porcelain module从长生不老药中进行互操作。在
所以我做了一个简单的例子:
我有一个长生不老药的功能是这样的:
defmodule PythonMessenger do
alias Porcelain.Process, as: Proc
alias Porcelain.Result
def test_messages do
proc = %Proc{pid: pid} =
Porcelain.spawn_shell("python ./python_scripts/reply_to_elixir.py",
in: :receive, out: {:send, self()})
Proc.send_input(proc, "Greetings from Elixir\n")
data = receive do
{^pid, :data, :out, data} -> data
end
IO.inspect data
Proc.send_input(proc, "Elixir: I heard you said \"#{data}\"\n")
data = receive do
{^pid, :data, data} -> data
end
IO.inspect data
Proc.send_input(proc, "Please quit\n")
data = receive do
{^pid, :data, data} -> data
end
IO.inspect data
end
end
以及如下所示的python脚本:
^{pr2}$但这行不通。python脚本永远不会退出。 如果只读一行:
line = sys.stdin.readline()
它很好用。在
有什么问题吗,有什么想法吗?在
您需要传递
-u
来禁用sys.stdin.readline()
中的缓冲。在交互式运行程序时,您不会看到这一点,但是当程序在没有TTY的情况下生成时,您将看到它。由于默认的缓冲区,Python进程没有为"Greetings from Elixir\n"
之类的短消息打印任何内容,而且由于receive
表达式,Elixir代码永远阻塞,等待Python进程打印某些内容。在来自
man python
:你在第二和第三个
^{pr2}$receive
模式中也有一些错误。以下是对我有用的代码:输出:
相关问题 更多 >
编程相关推荐