steffend

steffend

Phoenix Core Team

Opening a file on a remote node

Hello there,

reading the section about “IO and the file system”, I’ve noticed the following paragraph:

By modeling IO devices with processes, the Erlang VM allows I/O messages to be routed between different nodes running Distributed Erlang or even exchange files to perform read/write operations across nodes. Neat!

I’ve tried opening a file on a remote node using :rpc.call, but the returned pid seems to get closed immediately afterwards:

steffen@sdBookAir ~> iex --sname "b"
Erlang/OTP 23 [erts-11.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [dtrace]

Interactive Elixir (1.11.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(b@sdBookAir)1> Node.connect(:"a@sdBookAir")
true
iex(b@sdBookAir)1> {:ok, file} = :rpc.call(:a@sdBookAir, File, :open, ["/tmp/testfile"])
{:ok, #PID<11250.126.0>}
iex(b@sdBookAir)2> :file.read(file, 100)
{:error, :terminated}

Opening the file on the other node and then “transferring” the pid works:

iex(a@sdBookAir)1> {:ok, pid} = File.open("/tmp/testfile")
{:ok, #PID<0.128.0>}
# then on the other node
iex(b@sdBookAir)3> file = pid("11250.128.0")
#PID<11250.128.0>
iex(b@sdBookAir)4> :file.read(file, 100)
{:ok, "hello\n"}

Is there a way to prevent the file opened with :rpc.call from being closed? And if yes, why is it being closed in the first place?

Thanks!

Marked As Solved

Nicd

Nicd

rpc:call/4 documentation states that:

You cannot make any assumptions about the process that will perform the apply(). It may be the calling process itself, an rpc server, another server, or a freshly spawned process.

File.open/2 documentation states that:

io_device is actually the PID of the process which handles the file. This process monitors the process that originally opened the file (the owner process). If the owner process terminates, the file is closed and the process itself terminates too. If any process to which the io_device is linked terminates, the file will be closed and the process itself will be terminated.

These two combined, I think what happened is that your RPC call started a new process to open the file, then the process was finished and terminated, which also closed the file. Now that you are using a GenServer, it stays alive and thus the opened file also stays open.

Also Liked

steffend

steffend

Phoenix Core Team

If anyone is wondering, for now I’m using a GenServer to open the files, something like this:

defmodule RemoteFileServer do
  use GenServer

  @impl true
  def init(_) do
    {:ok, %{files: []}}
  end

  @impl true
  def handle_call({:open_file, path, modes}, _from, state) do
    {:ok, file} = File.open(path, modes)
    {:reply, file, update_in(state, [:files], fn files -> [file | files] end)}
  end

  def start_link(_) do
    GenServer.start_link(__MODULE__, [], name: {:global, {__MODULE__, Node.self()}})
  end

  def open(server, path, modes \\ [:read, :binary]) do
    GenServer.call(server, {:open_file, path, modes})
  end
end

Using the pid that the open function returns works perfectly transparent across nodes :smiley:

Where Next?

Popular in Questions Top

LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement