Is it safe to call IO.write from multiple processes?

I am aggregating data from different sources into a text file, the order does not matter. I have one entry per line.

Is it safe to call IO.write, to write to the file, from multiple GenServer processes (one for each source) ?

Yes, there’s only one process that actually writes to the file. IO.write works by sending a message to that process.

https://hexdocs.pm/elixir/File.html#module-processes-and-raw-files

1 Like

Just be sure you are calling the same Pid on all your calls to IO.write.

The problem you might have is related to pending the messages to be processed. The message queue from your process to write can become really big with time if you are sending messages to the writer process faster than it takes to write the text on the file, But you should not have problem with concurrency.

1 Like

The details are described here in the Erlang docs. It’s quite cunning I actually implemented iteratees before someone came along and gave them a name and wrote a paper about them. Sometimes we were pretty cunning. :wink:

7 Likes

Thanks! Somehow I was not 100% sure after reading the IO doc.