# List of what actions can cause a task switch?

**URL:** <https://discourse.julialang.org/t/list-of-what-actions-can-cause-a-task-switch/26751>\
**Category:** General Usage\
**Created:** [July 24, 2019, 6:08pm UTC](https://discourse.julialang.org/t/list-of-what-actions-can-cause-a-task-switch/26751 "2019-07-24T18:08:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [July 24, 2019, 6:08pm UTC](https://discourse.julialang.org/t/list-of-what-actions-can-cause-a-task-switch/26751/1 "2019-07-24T18:08:14Z")

</div>

What I wanted to do was run multiple external commands simultaneously and collect the results…so my first thought was to use Distributed creating N workers and have them execute the commands and return the data via a RemoteChannel. But then I got to wondering about using a run(`cmd`) in a task, and if I could have multiple tasks running at the same time.

So I wrote a short test:

```julia
function test(i)
    println("Starting $(i)")
    run(`sleep 10`)
    println("Finished $(i)")
end

for i in 1:10
    @async test(i)
end

```

And to my surprise they all run at the same time, which I didn’t really expect.

So my question is **which** library method can cause a “task” switch? If I read from a file, could a task switch occur? What about reading from a network socket? I **know** yield() can cause a task switch, but have no clue what other operations may. And it doesn’t look like the documentation for run() mentions that it yields or could yield.

At the core of this program I’m working on I have a set of methods that abstract random read/write access to a file, and it would really screw me up if those methods “yielded” in the middle of execution. Or should I just put them all in a big mutex even when I’m not using threads? Do mutexes even honor the Task scheduling!?!?!?

I suspect this might just be teething issue, i.e. once threads are out of the experimental stage it will be YES you need a mutex you idiot. However at the moment the julia process **is** single threaded which usually means you don’t need to “worry” about the atomic operations and locking…unless like I said a method yields when you don’t expect it to…

---

<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:** [July 24, 2019, 6:16pm UTC](https://discourse.julialang.org/t/list-of-what-actions-can-cause-a-task-switch/26751/2 "2019-07-24T18:16:19Z")

</div>

> [@pixel27](#):
>
> So my question is **which** library method can cause a “task” switch? If I read from a file, could a task switch occur?

Besides explicit calls to `yield` and other synchronization (locks, `wait`, etc.), you can have task switches on any I/O (files, networks, terminals, …), or calls to `run` and other functions that wait for a process to finish. (Some packages might also hook into this, e.g. ZMQ.jl does.)

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [July 24, 2019, 6:27pm UTC](https://discourse.julialang.org/t/list-of-what-actions-can-cause-a-task-switch/26751/3 "2019-07-24T18:27:21Z")

</div>

Okay, that makes sense, thanks.
