# Save a vector to a txt file asynchronously

**URL:** <https://discourse.julialang.org/t/save-a-vector-to-a-txt-file-asynchronously/94234>\
**Category:** New to Julia\
**Created:** [February 7, 2023, 8:51pm UTC](https://discourse.julialang.org/t/save-a-vector-to-a-txt-file-asynchronously/94234 "2023-02-07T20:51:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![x13420x](https://avatars.discourse-cdn.com/v4/letter/x/ecc23a/32.png) [@x13420x](https://discourse.julialang.org/u/x13420x)\
**Post date:** [February 7, 2023, 8:51pm UTC](https://discourse.julialang.org/t/save-a-vector-to-a-txt-file-asynchronously/94234/1 "2023-02-07T20:51:24Z")

</div>

Everytime I want to save a vector to a txt file the code is hanging…I need a copy of the vector to be passed to function and for it to be saved while the code is running. The saving is only taking a 10th of a second which is not accounting for about a 10x slowdown in julia from java…not sure if this is due to using bigints?

This code was written by chatgpt but the code still hangs…I dont need true concurency just for the compiler to not hang while the file is saving…

```julia
using Random, Primes, BenchmarkTools, DelimitedFiles, Profile

function savelr(vec::Vector{Int64}, fn::String)
    save_task = @async begin
		writedlm(fn, vec)
	end
    fetch(save_task)
	println("saved $fn")
end

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [February 7, 2023, 9:33pm UTC](https://discourse.julialang.org/t/save-a-vector-to-a-txt-file-asynchronously/94234/2 "2023-02-07T21:33:40Z")

</div>

I’m not entirely clear what you’re trying to do here, but spawning one task and then waiting for it is kind of pointless. This function is functionally equivalent to this:

```julia
using Random, Primes, BenchmarkTools, DelimitedFiles, Profile

function savelr(vec::Vector{Int64}, fn::String)
    writedlm(fn, vec)
    println("saved $fn")
end
```

---

<div class="post-metadata">

**Author:** ![x13420x](https://avatars.discourse-cdn.com/v4/letter/x/ecc23a/32.png) [@x13420x](https://discourse.julialang.org/u/x13420x)\
**Post date:** [February 7, 2023, 10:13pm UTC](https://discourse.julialang.org/t/save-a-vector-to-a-txt-file-asynchronously/94234/3 "2023-02-07T22:13:04Z")

</div>

This code works but I feel it is stalling …so maybe it does need its own thread…I wonder if this is what is causing my code to run so slow…

Also maybe there is a cleaner way to do this…I was watching someone code a quadratic sieve in java and I noticed he would wrap this stuff in a lambda function(I think)

```julia
function savelr(vec::Vector{Int64}, fn::String)
  	@async (Asavelr(vec,fn))
end
function Asavelr(vec::Vector{Int64}, fn::String)
  writedlm(fn, vec)
  println("saved $fn")
end

```

PS and if I just call Asavelr directly I will run into a problem where my vector changes before I can save it. Not sure if this works in Julia or not…if Julia is passing a pointer to my array then I am in problems…hehe

Okay this might actually be working because I tried commenting out the file writing and the code still hangs so its another issue…My question is…is this a correct way of saving files asychronously?

---

<div class="post-metadata">

**Author:** ![torrance](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torrance/32/38990_2.png) [@torrance](https://discourse.julialang.org/u/torrance)\
**Post date:** [February 8, 2023, 4:26am UTC](https://discourse.julialang.org/t/save-a-vector-to-a-txt-file-asynchronously/94234/4 "2023-02-08T04:26:29Z")

</div>

> [@x13420x](#):
>
> PS and if I just call Asavelr directly I will run into a problem where my vector changes before I can save it. Not sure if this works in Julia or not…if Julia is passing a pointer to my array then I am in problems…hehe

You will still encounter this problem with the code as it is. Julia [‘passes by sharing’](https://docs.julialang.org/en/v1/manual/functions/#Argument-Passing-Behavior), so in this case any mutations to your `vec` array will affect the writing.

If you really need asynchronous writing of the array whilst you go on to do other work, this will work just fine:

`@async writedlm(filename, copy(vector))`

Or alternatively run that in its own thread:

`@Threads.spawn writedlm(filename, copy(vector))`

Note that if the contents of the vector was not `isbits`, you would need to use `deepcopy()`.

Having said all that, unless the writing really is a bottleneck to your code (benchmark!), the simpler, synchronous version will make life easier.

---

<div class="post-metadata">

**Author:** ![x13420x](https://avatars.discourse-cdn.com/v4/letter/x/ecc23a/32.png) [@x13420x](https://discourse.julialang.org/u/x13420x)\
**Post date:** [February 8, 2023, 4:35am UTC](https://discourse.julialang.org/t/save-a-vector-to-a-txt-file-asynchronously/94234/5 "2023-02-08T04:35:32Z")

</div>

Thanks for the tips…I am playing with a quadratic sieve so it’s very computationaly intensive. I really do think it’s necesary to do at least the file saving asychronasly.

The vector is of int64s and in java passing a copy doesnt work You actually need to pass the copy of the vector to a function and then call the thread inside the function.

---

<div class="post-metadata">

**Author:** ![x13420x](https://avatars.discourse-cdn.com/v4/letter/x/ecc23a/32.png) [@x13420x](https://discourse.julialang.org/u/x13420x)\
**Post date:** [February 13, 2023, 6:50pm UTC](https://discourse.julialang.org/t/save-a-vector-to-a-txt-file-asynchronously/94234/6 "2023-02-13T18:50:48Z")

</div>

Ok I tried doing the deepcopy and it works…seems to be faster also…The problem in Java was that I was using copy and not deepcopy.

`@async writedlm("lr$(deepcopy(happy)).txt",deepcopy(lr))`

I dont know if its a problem but when I read from the file it gives me a matrix…which was causing me errors for my save file function(which was waiting for a vector) which is why I am converting to your way.

And my code is running as fast or faster than Java…The file writing is orders of magnatude faster because I was using the processing library to write my code.

---

<div class="post-metadata">

**Author:** ![x13420x](https://avatars.discourse-cdn.com/v4/letter/x/ecc23a/32.png) [@x13420x](https://discourse.julialang.org/u/x13420x)\
**Post date:** [February 14, 2023, 2:47pm UTC](https://discourse.julialang.org/t/save-a-vector-to-a-txt-file-asynchronously/94234/7 "2023-02-14T14:47:12Z")

</div>

Turns out the deepcopy command is happening asynchronously which means your way of doing it will work most times but the code below is broken using your method:

```julia
|||||@async writedlm(gcdcomp.txt,deepcopy(gcdcomp))|
|---|---|---|---|---|
|||||@async writedlm(newprimes.txt,deepcopy(np))|
|||||@async writedlm(mc.txt,deepcopy(mc))|
|||||println(Saving the state of the program.....)|
|||||push!(gcdcomp,rm)|

```

The push statement executes before the deepcopy which means this is not a secure way to save asychronously…you need to send the array to a function…you say this shouldnt work but it does…maybe the compiler is smart enough to understand what I want to do…not sure…also a self correction java does not have a deepcopy command…I was thinking of clone which only passes a reference to the original array.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [February 14, 2023, 3:36pm UTC](https://discourse.julialang.org/t/save-a-vector-to-a-txt-file-asynchronously/94234/8 "2023-02-14T15:36:37Z")

</div>

You’re explicitly asking for those entire expressions to be evaluated asynchronously…

---

<div class="post-metadata">

**Author:** ![x13420x](https://avatars.discourse-cdn.com/v4/letter/x/ecc23a/32.png) [@x13420x](https://discourse.julialang.org/u/x13420x)\
**Post date:** [February 14, 2023, 6:01pm UTC](https://discourse.julialang.org/t/save-a-vector-to-a-txt-file-asynchronously/94234/9 "2023-02-14T18:01:24Z")

</div>

Exactly!!! everything to the right executes asynchronously and thats the problem with [torrance](https://discourse.julialang.org/u/torrance)´s solution…However he did make a very helpful point about the arrays needing to be passed as deepcopys…because I wasnt doing that my linear relation number 6 was being stored in lr5.txt for some reason…My new code is as follows…I think this will probably be the end of the discusion:

```julia
function savelr(vec, fn)
  	@async writedlm(fn, vec)
end

savelr(deepcopy(lr),"lr$(deepcopy(filename)).txt")

```

Thanks a lot for the help…
