# Updating an XLSX file simultaneously by more than one Julia script

**URL:** https://discourse.julialang.org/t/updating-an-xlsx-file-simultaneously-by-more-than-one-julia-script/105397
**Category:** Data
**Tags:** question, data, xlsx, excel
**Created:** [October 25, 2023, 8:11pm UTC](https://discourse.julialang.org/t/updating-an-xlsx-file-simultaneously-by-more-than-one-julia-script/105397 "2023-10-25T20:11:30Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)
#### Post date: [October 25, 2023, 8:11pm UTC](https://discourse.julialang.org/t/updating-an-xlsx-file-simultaneously-by-more-than-one-julia-script/105397/1 "2023-10-25T20:11:30Z")

</div>

I have a Julia script which does some calculations and in the end, the output is written to an Excel file using the following function:

```julia
function populate_spreadsheet(sol, instance_number, sheet_number, stat::Symbol)
    XLSX.openxlsx("test.xlsx", mode = "rw") do xf
        sheet = xf[sheet_number]
        if stat == :obj
            sheet[string("A", instance_number)] = sol.objective
        elseif stat == :gap
            sheet[string("B", instance_number)] = sol.gap
        elseif stat == :time
            sheet[string("C", instance_number)] = sol.solvetime
        end
    end
end

```

This works perfectly fine when I run a single instance of my script (say for `instance_number=1`); however, I start to run into problems when I run multiple instances of the script at once. I am using an HPC cluster, and run an array of jobs using a slurm script. For example, I run an array of jobs for `instance_number` 1 to 40. The errors are not consistent. Two of the most common errors I get are `unable to parse XML file` or `AssertionError: Couldn't find xl/sharedStrings.xml `. I suspect it is because the independent jobs are trying to access and write to the file at once. Is there a way to overcome this problem? Any help is appreciated. Thanks!

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [October 25, 2023, 8:23pm UTC](https://discourse.julialang.org/t/updating-an-xlsx-file-simultaneously-by-more-than-one-julia-script/105397/2 "2023-10-25T20:23:06Z")

</div>

Writing to the same file on disk with multiple processes is always tricky. Can’t you write to multiple files in parallel and then add a final post-processing step with the main process that simply merges the results?

---

<div class="post-metadata">

### Author: ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)
#### Post date: [October 25, 2023, 8:27pm UTC](https://discourse.julialang.org/t/updating-an-xlsx-file-simultaneously-by-more-than-one-julia-script/105397/3 "2023-10-25T20:27:05Z")

</div>

Yes, that’s definitely one way I was thinking, where I generate one XLSX file per job and then just run a script to merge all data into one XLSX file; however, I want to avoid generating so many extra files (I have a lot more parallel jobs – somewhere around 200). So 200 XLSX files don’t sound like the most optimal way to go about this. 😅

---

<div class="post-metadata">

### Author: ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)
#### Post date: [October 25, 2023, 8:28pm UTC](https://discourse.julialang.org/t/updating-an-xlsx-file-simultaneously-by-more-than-one-julia-script/105397/4 "2023-10-25T20:28:17Z")

</div>

If you want to stick to job arrays, I would write out one file per job to a temporary directory, then combine them at the end with a dependent job. 200 XLSX files is hardly a sin compared to a lot of the hacks that are required to get things to cooperate on HPC systems. There are more-complicated ways to do this with tools like Dagger.jl or [parallel HDF5](https://juliaio.github.io/HDF5.jl/stable/mpi/#Reading-and-writing-data-in-parallel) if you don’t mind switching file formats and using MPI.

---

<div class="post-metadata">

### Author: ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)
#### Post date: [October 25, 2023, 8:31pm UTC](https://discourse.julialang.org/t/updating-an-xlsx-file-simultaneously-by-more-than-one-julia-script/105397/5 "2023-10-25T20:31:18Z")

</div>

OK. I guess than I would do it the easy, more intuitive way of generating one file per job and then merging it. Thanks! 🙂

---

<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: [October 26, 2023, 12:05pm UTC](https://discourse.julialang.org/t/updating-an-xlsx-file-simultaneously-by-more-than-one-julia-script/105397/6 "2023-10-26T12:05:13Z")

</div>

> [@juliohm](#):
>
> Writing to the same file on disk with multiple processes is always tricky.

For reference, you can use the [Pidfile standard library](https://docs.julialang.org/en/v1/stdlib/FileWatching/#Pidfile) to manage exclusive access to files.
