# Atomic write(filename) append?

**URL:** <https://discourse.julialang.org/t/atomic-write-filename-append/9560>\
**Category:** General Usage\
**Created:** [March 7, 2018, 6:19am UTC](https://discourse.julialang.org/t/atomic-write-filename-append/9560 "2018-03-07T06:19:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [March 7, 2018, 6:19am UTC](https://discourse.julialang.org/t/atomic-write-filename-append/9560/1 "2018-03-07T06:19:52Z")

</div>

dear julia wizards—is it possible to procure an atomic write-append that briefly locks an append-output file in order to prevent other processes/threads from interleaving output? the natural way would be safety in

```julia
write("/tmp/myatomic.txt", "hello\n"; append=true, lockme=true)

```

the julia history file probably needs file-locking support from the OS to prevent multiple julia master processes interleaving. alas, I am just asking whether slaves from the same julia master process can safely write short snippets to the same file without stepping on one another.

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [March 7, 2018, 2:00pm UTC](https://discourse.julialang.org/t/atomic-write-filename-append/9560/2 "2018-03-07T14:00:29Z")

</div>

This might be a bit overkill, but it probably should work

```julia
z = [IOBuffer() for i in 1:10] ##@everywhere?
write(z[thread_id?], "somedata");

```

You could interleave by newlines or use another sort of marker with `split()`, or just stack them whole

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 7, 2018, 2:38pm UTC](https://discourse.julialang.org/t/atomic-write-filename-append/9560/3 "2018-03-07T14:38:53Z")

</div>

> [@iwelch](#):
>
> I am just asking whether slaves from the same julia master process can safely write short snippets to the same file without stepping on one another.

In general, I would suggest that the worker processes send messages to a `RemoteChannel` on the the master process, and let the master process do I/O (in an asynchronous `Task`). This serializes the output without involving file locking. It is also advantageous because in some cluster situations, worker processes may not even have access to the file system, and even if you have a distributed filesystem, locking is very tricky. It also allows the “file” to be something else, like a pipe or a terminal.
