Make pulumi.runtime.invoke
synchronous.
#3019
Merged
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.
These changes make the
pulumi.runtime.invoke
function invokable in asynchronous manner. Because this function still needs to perform
asynchronous work under the covers--namely awaiting a provider URN and
ID if a provider instance is present in the
InvokeOptions
--thisrequires some creativity. This creativity comes in the form of a helper
function,
_sync_await
, that performs a logical yield from thecurrently running event, manually runs the event loop until the given
future completes, performs a logical resume back to the
currently executing event, and returns the result of the future.
The code in
_sync_await
is a bit scary, as it relies upon knowledge of(and functions in) the internals of the
asyncio
package. The necessarywork performed in this function was derived from the implementations of
task_step
(which pointed out the need to call_{enter,leave}_task
)and
BaseEventLoop.run_forever
(which illustrated how the event loop ispumped). In addition to potential breaking changes to these internals,
the code may not work if a user has provided an alternative implementation
for
EventLoop
. That said, the code is a close enough copy ofBaseEventLoop.run_forever
that it should be a reasonable solution.