# Background processes in julia

**URL:** https://discourse.julialang.org/t/background-processes-in-julia/11596
**Category:** General Usage
**Created:** [June 12, 2018, 1:15am UTC](https://discourse.julialang.org/t/background-processes-in-julia/11596 "2018-06-12T01:15:07Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![00vareladavid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00vareladavid/32/23521_2.png) [@00vareladavid](https://discourse.julialang.org/u/00vareladavid)
#### Post date: [June 12, 2018, 1:15am UTC](https://discourse.julialang.org/t/background-processes-in-julia/11596/1 "2018-06-12T01:15:07Z")

</div>

I’m porting some bash code to julia but I’m not sure the best way to create background processes in julia.

My bash code is something like:

```julia
func1 &
func2 &
wait

```

This will start func1 and func2 concurrently. I saw that run(`func1` & `func2`) would run two external commands concurrently. But what if func1 and func2 have already been ported to Julia?

EDIT:  
Specifically, `func1` and `func2` are meant to run indefinitely (at least until I send some kind of teardown signal).  
Also, I have taken a look at the [parallel](https://docs.julialang.org/en/latest/base/parallel/) and [running external programs](https://docs.julialang.org/en/latest/manual/running-external-programs/) documentation, but I’m still not sure of the best way to go about this.

---

<div class="post-metadata">

### Author: ![gpapo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gpapo/32/1857_2.png) [@gpapo](https://discourse.julialang.org/u/gpapo)
#### Post date: [June 12, 2018, 8:52am UTC](https://discourse.julialang.org/t/background-processes-in-julia/11596/2 "2018-06-12T08:52:51Z")

</div>

```julia
@sync begin
    @async f1
    @async f2
end

```

See [asynchronous - How and When to Use @async and @sync in Julia - Stack Overflow](https://stackoverflow.com/questions/37287020/how-and-when-to-use-async-and-sync-in-julia).

---

<div class="post-metadata">

### Author: ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)
#### Post date: [June 12, 2018, 10:47am UTC](https://discourse.julialang.org/t/background-processes-in-julia/11596/3 "2018-06-12T10:47:00Z")

</div>

`@async` is right if they are IO bound task.  
It doesn’t do true parallelism, it does co-routines kinda parallelism.  
Only one task runs at a time,  
but `yield`s then the other gets to run.  
`yield`ing is done whenever a task is waiting for IO.  
It can be done other times, but IO is the main one that exists by default.  
Others include when external processes are run (IIRC) and when waiting for Channels to get items/be not full of items (because its kinda what they are for, Channels exist for communicating between async tasks, one task puts data into a channel the other reads it out, when the channel is empty then better yield so other task can run to put stuff on. and visa versa).  
And `@sync` will yield from the main task until all the enclosed `@asyncs` are complete

If you actually need them to run at the same time then you want to use `@spawn` instead of `@async` and you want to do the stuff from the parallel section of the manual

---

<div class="post-metadata">

### Author: ![felipenoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/felipenoris/32/553_2.png) [@felipenoris](https://discourse.julialang.org/u/felipenoris)
#### Post date: [September 16, 2018, 12:28am UTC](https://discourse.julialang.org/t/background-processes-in-julia/11596/4 "2018-09-16T00:28:02Z")

</div>

" `yield` ing is done whenever a task is waiting for IO."

How does this work? I mean, how can you tell that a task yields because it is waiting on IO? If I run a `ccall` on a external C function that does IO, the julia task will call `yield`?
