# Parallel execution of functions outside a loop is possible in Julia?

**URL:** <https://discourse.julialang.org/t/parallel-execution-of-functions-outside-a-loop-is-possible-in-julia/1091>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 21, 2016, 2:14pm UTC](https://discourse.julialang.org/t/parallel-execution-of-functions-outside-a-loop-is-possible-in-julia/1091 "2016-12-21T14:14:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fred](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred/32/14175_2.png) [@Fred](https://discourse.julialang.org/u/Fred)\
**Post date:** [December 21, 2016, 2:14pm UTC](https://discourse.julialang.org/t/parallel-execution-of-functions-outside-a-loop-is-possible-in-julia/1091/1 "2016-12-21T14:14:39Z")

</div>

Hi !

In Julia 0.5 it is possible to parallelize a function in a loop :

```julia
   @parallel (+) for i=1:100

   Threads.@threads for i = 1:10

```

or to apply a function to all elements in some collection :  
` pmap(f, c)`

But is it possible to execute independent functions in parallel outside a loop, like goroutines in golang for example?

```julia
    go func1(x, channel1)
    go func2(y, channel2)

```

Thank you very much for your comments !

---

<div class="post-metadata">

**Author:** ![Muninn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/muninn/32/361_2.png) [@Muninn](https://discourse.julialang.org/u/Muninn)\
**Post date:** [December 21, 2016, 2:55pm UTC](https://discourse.julialang.org/t/parallel-execution-of-functions-outside-a-loop-is-possible-in-julia/1091/2 "2016-12-21T14:55:07Z")

</div>

Yep! Have a look at @spawn and @spawnat which you can find more information about here: [http://docs.julialang.org/en/release-0.5/stdlib/parallel/#Base.@spawn](http://docs.julialang.org/en/release-0.5/stdlib/parallel/#Base.@spawn)

If you have multiple processes running for Julia you can do

```julia
func1_future = @spawn func1(x, channel1)
func2_future = @spawn func2(y, channel2)

```

These return a Future so to get the return value you need to do a fetch which blocks until the value is ready like so

```julia
func1_result = fetch(func1_future)
func2_result = fetch(func2_future)

```

You can find information about fetch here: [http://docs.julialang.org/en/release-0.5/stdlib/parallel/#Base.fetch](http://docs.julialang.org/en/release-0.5/stdlib/parallel/#Base.fetch)

And some general information on parallelism in Julia can be found here: [http://docs.julialang.org/en/release-0.5/manual/parallel-computing/](http://docs.julialang.org/en/release-0.5/manual/parallel-computing/)

---

<div class="post-metadata">

**Author:** ![Fred](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred/32/14175_2.png) [@Fred](https://discourse.julialang.org/u/Fred)\
**Post date:** [December 21, 2016, 4:32pm UTC](https://discourse.julialang.org/t/parallel-execution-of-functions-outside-a-loop-is-possible-in-julia/1091/3 "2016-12-21T16:32:33Z")

</div>

@Muninn Great ! I will try that ! Thank you very much !
