Elixir / Erlang: connection with the external process

Let's say I have a simple python script that executes an elixir / erlang script module using a module subprocess.

Assume the PID for python script is equal P1, and for a running project, elixir / erlang script is P2.

I want to know if a connection is possible between P1and P2. More specifically, P1it records something stdinfrom P2, and P2reads out the received input P1, and writes appropriate output to its own stdoutand P1reads out stdoutof P2and again writes something in stdinof P2, etc.

I know that another way is possible, i.e. spawning of the external process from within the elixir / erlang, and then communication with the process. Any help appreciated, thanks.

+4
source share
2 answers

, IPC . ( Qaru!) , , , , - , Erlang/Elixir Python, Python Erlang/Elixir. ( , Erlang Elixir, Python), ! Badu , Elixir Port .

, , , , . , , Erlang Python!

-, Python (eip.py):

#!/usr/bin/env python
from subprocess import Popen, PIPE

erl = Popen(['escript', 'eip.escript'],
            stdin=PIPE, stdout=PIPE, stderr=PIPE)
ping = input('Ping: ')

outs, errs = erl.communicate(input=ping.encode('utf-8'),
                                 timeout=15)

print(outs.decode('utf-8'))

Erlang ( Python) - escript, , eip.escript:

#!/usr/bin/env escript

main(_Args) ->
  Ping = io:get_line(""),
  io:format("Pong: ~ts", [Ping]).

, python3 eip.py asdf Ping:, Pong: asdf.


Elixir : Mix , Mix escript. , :

$ mix new eip
* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating lib
* creating lib/eip.ex
* creating test
* creating test/test_helper.exs
* creating test/eip_test.exs

Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:

    cd eip
    mix test

Run "mix help" for more commands.

(, Mix , , - , )

escript mix.exs, :

defmodule Eip.MixProject do
  use Mix.Project

  def project, do: [
    app: :eip,
    version: "0.1.0",
    elixir: "~> 1.9",
    start_permanent: Mix.env() == :prod,
    deps: deps(),
    escript: escript()
  ]

  def application, do: [extra_applications: [:logger]]

  defp deps, do: []
  defp escript, do: [main_module: Eip]
end

, , lib/eip.ex:

defmodule Eip do
  def main(_argv) do
    ping = IO.gets("")
    IO.puts("Pong: #{ping}")
  end
end

:

$ mix escript.build
Compiling 1 file (.ex)
Generated eip app
Generated escript eip with MIX_ENV=dev

eip.py , Elixirified ping/pong IPC thingamabob:

#!/usr/bin/env python
from subprocess import Popen, PIPE, TimeoutExpired

erl = Popen(['escript', 'eip/eip'],
            stdin=PIPE, stdout=PIPE, stderr=PIPE)
ping = input('Ping: ')

outs, errs = erl.communicate(input=ping.encode('utf-8'))

print(outs.decode('utf-8'))

, :

$ python3 eip.py
Ping: asdf
Pong: eof

, Erlang (.. IO.gets("") :io.get_line("") IO.puts("Pong: #{ping}") :io.fwrite("Pong: ~ts", [ping]), - Elixir STDIN , , . , , !

+2

, . Erlport - Elixir Python

0

Source: https://habr.com/ru/post/1672436/


All Articles