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

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.

@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.

1 Like

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!

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.

1 Like