@async macro not recommended?

@async and @spawn actually do almost the very same thing: They wrap the contained code in a Task and schedule it. The only difference is in the scheduling. @async sets the sticky flag, which means the Task is not allowed to migrate to another thread and this then extends to the parent Task that spawned (and its parent and so on). This is generally unwanted because it affects load-balancing. Julia is actually one of very few languages where threading composes well due to its dynamic, depth-first scheduler but that requires Tasks to be able to change their worker threads. Read more about it here:

To summarize: The core problem is that @async can cause tremendous slowdowns in unrelated code parts because it disables migration of Tasks which is needed for efficient, composable multi-threading.

This model is wrong. Julia never adds threads dynamically. There is a fixed number of worker threads that switch between all scheduled Tasks. You could view the creation of a Task as similar to spawning a green thread.

9 Likes