# How to parallelize within a function

**URL:** https://discourse.julialang.org/t/how-to-parallelize-within-a-function/69082
**Category:** New to Julia
**Tags:** parallel
**Created:** [October 2, 2021, 12:34am UTC](https://discourse.julialang.org/t/how-to-parallelize-within-a-function/69082 "2021-10-02T00:34:17Z")
**Posts on this page:** 1
**Showing post:** 5

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [October 2, 2021, 3:18am UTC](https://discourse.julialang.org/t/how-to-parallelize-within-a-function/69082/5 "2021-10-02T03:18:09Z")

</div>

> [@jling](#):
>
> a lot of things are thread-safe

Uh… I don’t think this is true yet 😅 actually. Some things (e.g., `Array`) work great but many things are hard to reason about the safety (e.g., sparse matrix). Importantly, there’s no documentation on what is safe when.

> [@jling](#):
>
> Julia has true threads

Sorry to nitpick, but I’d be careful about such a claim. For some definitions of “true threads”, one can argue Python has “more true” threads than Julia. For example, Python has a more transparent OS thread API than Julia; Julia only has tasks (and that’s kinda the point). But unfortunately to Python programmers, it was designed in 90s where threads for parallelism were not a thing (at least not for everyone) and so it’s useful mainly for I/O or GIL-releasing external code. On the other hand, Julia has “more true” threads than Python in another sense if one defines “threads” as a synonym of shared-memory parallelism.

> [@Soldalma](#):
>
> I believe it can be done with threads but I was somewhat wary of doing it for the reasons above. In Python and R I always used processes.

If you have a rough idea of the applicability of process-based parallelism in the problem you have, I think starting from your comfort zone sounds like a good idea.

Going back to the problem in the OP, it’s not a good idea to use `@everywhere` inside a function. It’s mainly for “static things” like `using Package` and `include("script.jl")`. You’d probably want to use `remotecall` here (and maybe iterate over the worker ids returned from `workers()`).

Also, please quote your code: [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)

---

_[View the full topic](https://discourse.julialang.org/t/how-to-parallelize-within-a-function/69082)._
