allow integrating with external event loop - #531
Merged
Conversation
Coverage Report for CI Build 970Coverage decreased (-0.007%) to 76.222%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
Guest0x0
force-pushed
the
external-loop-integration
branch
from
August 5, 2026 10:29
62e65b2 to
ea6e575
Compare
Guest0x0
force-pushed
the
external-loop-integration
branch
from
August 6, 2026 07:31
ff5bf1f to
d46f5fe
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
closes #526
moonbitlang/asyncrelies on an event loop to perform asynchronous IO operations internally. However, some applications may have their own event loop as well. For example, GUI frameworks typically has their own event loop. Event loops are non-modular by nature, so to integrate two event loops, the only possible way is to run each loop in a dedicated thread, and use a self-pipe or similar to communicate between the two loops. Some libraries require their event loop being run in the main thread. In that case, the only possible workaround before this PR is to run all MoonBit code in a dedicated thread, which is tedious.This PR add native support for such external loop integration. A new API,
@async.set_external_event_loop, is introduced, which enables external event loop integration by providing an event loop implementation. The implementation is abstracted via a trait@moonbitlang/async/external_loop_integration.ExternalEventLoop. The user-provided loop should provide its wait function with timeout support, and a callback for waking the loop for use bymoonbitlang/async. See the documents of that trait for more details on which operations are needed and semantic caveats.@async.set_external_event_loopcan be used withasync fn main. In fact it can be called inasync fn mainas well, as long as it is invoked before any actual async operations. Whenmoonbitlang/asyncsees that an external event loop is set, it automatically enter external loop integration mode. In this mode, the waiting part ofmoonbitlang/asyncevent loop will be spawned in a dedicated thread, and the user-provided loop will be run in the main thread instead.