Background processes in julia

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:

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 and running external programs documentation, but I’m still not sure of the best way to go about this.

@sync begin
    @async f1
    @async f2
end

See asynchronous - How and When to Use @async and @sync in Julia - Stack Overflow.

1 Like

@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 yields then the other gets to run.
yielding 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

5 Likes

" 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?