Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK/Python] Respect --parallel flag #11122

Merged
merged 3 commits into from Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,4 @@
changes:
- type: fix
scope: sdk/python
description: Python runtime now respects the --parallel flag.
12 changes: 12 additions & 0 deletions sdk/python/cmd/pulumi-language-python-exec
Expand Up @@ -3,11 +3,13 @@

import argparse
import asyncio
from typing import Optional
import logging
import os
import sys
import traceback
import runpy
from concurrent.futures import ThreadPoolExecutor

# The user might not have installed Pulumi yet in their environment - provide a high-quality error message in that case.
try:
Expand Down Expand Up @@ -51,6 +53,13 @@ def _get_user_stacktrace(user_program_abspath: str) -> str:
# we did not detect a __main__ program, return normal traceback
return traceback.format_exc()

def _set_default_executor(loop, parallelism: Optional[int]):
'''configure this event loop to respect the settings provided.'''
if parallelism is None:
return
exec = ThreadPoolExecutor(max_workers=parallelism)
loop.set_default_executor(exec)

if __name__ == "__main__":
# Parse the arguments, program name, and optional arguments.
ap = argparse.ArgumentParser(description='Execute a Pulumi Python program')
Expand Down Expand Up @@ -125,6 +134,9 @@ if __name__ == "__main__":
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

# Configure the event loop to respect the parallelism value provided as input.
_set_default_executor(loop, settings.parallel)

# We are (unfortunately) suppressing the log output of asyncio to avoid showing to users some of the bad things we
# do in our programming model.
Expand Down
7 changes: 3 additions & 4 deletions sdk/python/lib/pulumi/runtime/settings.py
Expand Up @@ -208,10 +208,6 @@ def get_engine() -> Optional[Union[engine_pb2_grpc.EngineStub, Any]]:
"""
return SETTINGS.engine


ROOT: ContextVar[Optional[Resource]] = ContextVar("root_resource", default=None)


def get_root_resource() -> Optional["Resource"]:
"""
Returns the implicit root stack resource for all resources created in this program.
Expand All @@ -226,6 +222,9 @@ def set_root_resource(root: "Resource"):
ROOT.set(root)


ROOT: ContextVar[Optional[Resource]] = ContextVar("root_resource", default=None)


async def monitor_supports_feature(feature: str) -> bool:
if feature not in SETTINGS.feature_support:
monitor = SETTINGS.monitor
Expand Down