# What exactly qualifies as „lexically enclosed use“ when using @sync?

**URL:** <https://discourse.julialang.org/t/what-exactly-qualifies-as-lexically-enclosed-use-when-using-sync/88922>\
**Category:** General Usage\
**Tags:** parallel, distributed\
**Created:** [October 18, 2022, 5:48pm UTC](https://discourse.julialang.org/t/what-exactly-qualifies-as-lexically-enclosed-use-when-using-sync/88922 "2022-10-18T17:48:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![told](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/told/32/17083_2.png) [@told](https://discourse.julialang.org/u/told)\
**Post date:** [October 18, 2022, 5:48pm UTC](https://discourse.julialang.org/t/what-exactly-qualifies-as-lexically-enclosed-use-when-using-sync/88922/1 "2022-10-18T17:48:44Z")

</div>

The documentation for `@sync` states that:

> Wait until all lexically-enclosed uses of `@async` , `@spawn` , `@spawnat` and `@distributed` are complete. All exceptions thrown by enclosed async operations are collected and thrown as a `CompositeException` .

For me it is however unclear what „lexically-enclosed“ means. Does e.g.

```julia
@sync @spawnat 2 include(„largefile.jl“)

```

Wait till the code within largefile has been completely included on the process or just wait till the include functions gets called? I ask since I think I might hit a racing condition with the above code.

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [October 18, 2022, 11:28pm UTC](https://discourse.julialang.org/t/what-exactly-qualifies-as-lexically-enclosed-use-when-using-sync/88922/2 "2022-10-18T23:28:54Z")

</div>

I think the docs are confusing there too. I’d assume that the macro searches the actual code for `@spawn` so your example wouldn’t work, but I haven’t checked. In that case, you’d probably have to wait for the task. Try making a pull request to the docs with a clarification and example!

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [October 19, 2022, 8:34am UTC](https://discourse.julialang.org/t/what-exactly-qualifies-as-lexically-enclosed-use-when-using-sync/88922/3 "2022-10-19T08:34:50Z")

</div>

> [@told](#):
>
> Does e.g.
> 
> ```julia
> @sync @spawnat 2 include(„largefile.jl“)
> 
> ```
> 
> Wait till the code within largefile has been completely included on the process or just wait till the include functions gets called?

No - lexically enclosed refers to “whatever the macro can see”. Since macros operate on the syntax level, before `include` is resolved/called (it’s treated like any other function), the macro cannot see what happens inside of the included file.

It’s helpful to use `@macroexpand` to see what a macro ends up doing.
