asyncdispatch thread-pool scheduler replaces async single-threaded loop in Nim 1.0
0 reputation · 06 Sept 2020, 07:59 UTC
0 reputation · 06 Sept 2020, 07:59 UTC
A small Nim application using the async module needs to be moved to Nim 1.0 without changing runtime behavior. The documented compatibility boundary is the introduction of asyncdispatch as the default scheduler in Nim 1.0, which replaces the older async module's single-threaded event loop with a thread-pool based implementation.
The change is recorded as a breaking change in the Nim 1.0 release notes. The legacy async module remains available as a deprecated wrapper that forwards to asyncdispatch, which creates an unresolved decision about long-term support. Code that assumed single-threaded execution ordering may behave differently under a multi-threaded pool, and the interaction between the wrapper and direct asyncdispatch usage is not clearly defined for library authors.
What is the recommended import strategy for new code targeting Nim 1.0 while preserving compatibility with earlier Nim releases? Is mixing asyncdispatch and the deprecated async wrapper in the same binary supported, and what scheduler initialization is guaranteed? Is there a documented deprecation timeline for removal of the legacy async wrapper?
34775 reputation · 06 Sept 2020, 14:24 UTC
asyncdispatch the default asynchronous scheduler.async module exists as a thin wrapper that forwards all calls to asyncdispatch and is marked deprecated.asyncdispatch.initScheduler; no explicit call is required.For code that targets Nim 1.0 and later, import asyncdispatch directly. To keep the same source compilable with pre‑1.0 Nim releases, wrap the import in a version check:
when defined(nimVersion) and nimVersion >= 100:
import asyncdispatch
else:
import async
This yields a single scheduler instance regardless of which branch is taken, because the async wrapper in older Nim simply delegates to the original single‑threaded loop, while the asyncdispatch branch uses the thread‑pool scheduler.
If a project accidentally imports both async and asyncdispatch (e.g., via a dependency), the Nim linker merges them to the same underlying scheduler; no duplicate initialization occurs and the behavior remains consistent with the scheduler selected by the active import.
The release notes for Nim 1.0 label the async module as deprecated but do not publish a specific removal date or version. The wrapper is expected to be dropped in a future major release after the community has had time to migrate, but no formal timeline is currently documented. Monitor the Nim changelog for any announcement.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.