diff --git a/src/rez/cli/env.py b/src/rez/cli/env.py index a917830db..081389648 100644 --- a/src/rez/cli/env.py +++ b/src/rez/cli/env.py @@ -117,9 +117,9 @@ def setup_parser(parser, completions=False): "--no-pkg-cache", action="store_true", help="Disable package caching") parser.add_argument( - "--pkg-cache-sync", action="store_true", - help="Disable asynchronous package caching. " - "Process will block until packages are cached.") + "--pkg-cache-mode", choices=["sync", "async"], + help="Optionally disable for force enable asynchronous package caching. " + "If 'sync', the process will block until packages are cached.") parser.add_argument( "--pre-command", type=str, help=SUPPRESS) PKG_action = parser.add_argument( @@ -202,6 +202,13 @@ def command(opts, parser, extra_arg_groups=None): rule = Rule.parse_rule(rule_str) package_filter.add_inclusion(rule) + if opts.pkg_cache_sync == "async": + package_cache_async = True + elif opts.pkg_cache_sync == "sync": + package_cache_async = False + else: + package_cache_async = None + # perform the resolve context = ResolvedContext( package_requests=request, @@ -217,7 +224,7 @@ def command(opts, parser, extra_arg_groups=None): suppress_passive=opts.no_passive, print_stats=opts.stats, package_caching=(not opts.no_pkg_cache), - package_cache_async=(not opts.pkg_cache_sync), + package_cache_async=package_cache_async, ) success = (context.status == ResolverStatus.solved) diff --git a/src/rez/package_cache.py b/src/rez/package_cache.py index 8e85ca7a1..01a0e0abf 100644 --- a/src/rez/package_cache.py +++ b/src/rez/package_cache.py @@ -366,6 +366,16 @@ def remove_variant(self, variant): return self.VARIANT_REMOVED + def add_variants_async(self, variants): + """Update the package cache by adding some or all of the given variants. + + This method is called when a context is created or sourced. Variants + are then added to the cache in a separate process. + + This method is left for backwards compatibility. + """ + return self.add_variants(variants, package_cache_async=True) + def add_variants(self, variants, package_cache_async=True): """Update the package cache by adding some or all of the given variants. @@ -467,7 +477,7 @@ def add_variants(self, variants, package_cache_async=True): **kwargs ) if not package_cache_async: - process.wait() + process.communicate() except Exception as e: print_warning( diff --git a/src/rez/resolved_context.py b/src/rez/resolved_context.py index 6018f7327..bad6be45b 100644 --- a/src/rez/resolved_context.py +++ b/src/rez/resolved_context.py @@ -248,7 +248,6 @@ def __init__(self, package_requests, verbosity=0, timestamp=None, package_caching = config.package_cache_during_build else: package_caching = True - self.package_caching = package_caching if package_cache_async is None: diff --git a/src/rez/tests/test_package_cache.py b/src/rez/tests/test_package_cache.py index 749f12ce5..01f91ff7d 100644 --- a/src/rez/tests/test_package_cache.py +++ b/src/rez/tests/test_package_cache.py @@ -140,6 +140,9 @@ def test_caching_on_resolve(self): "pyfoo-3.1.0" ]) + # Prove that the resolved context used async mode. + self.assertTrue(c.package_cache_async) + variant = c.get_resolved_package("timestamped") # Retry 50 times with 0.1 sec interval, 5 secs is more than enough for @@ -154,7 +157,8 @@ def test_caching_on_resolve(self): resolve_not_always_cached = True time.sleep(0.1) - self.assertNotEqual(cached_root, None) + self.assertNotEqual(cached_root, None, + msg="Packages were expected to be cached, but were not.") # Test that the package is not immediately cached, since it is asynchronous # WARNING: This is dangerous since it does open the test to a race condition and