Skip to content

[Python3] Make update-checkout work with Python 2 & 3 #33081

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

Merged
merged 5 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions utils/update-checkout
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import sys

import update_checkout

# The internal Windows implementation tries to import the current module using
# sys.modules[__name__]. This file (called 'update-checkout') is not a valid
# Python module name, as it contains a '-' and doesn't end with '.py'. This
# results in errors running update-checkout on Windows:'ImportError: No module
# named update-checkout'.
# As such, we need to manually set sys.modules[__name__] to a valid module
# identifier to work around these errors.
sys.modules[__name__] = sys.modules['update_checkout']
update_checkout.main()
if __name__ == '__main__':
# The internal Windows implementation tries to import the current module using
# sys.modules[__name__]. This file (called 'update-checkout') is not a valid
# Python module name, as it contains a '-' and doesn't end with '.py'. This
# results in errors running update-checkout on Windows:'ImportError: No module
# named update-checkout'.
# As such, we need to manually set sys.modules[__name__] to a valid module
# identifier to work around these errors.
sys.modules[__name__] = sys.modules['update_checkout']

update_checkout.main()
11 changes: 6 additions & 5 deletions utils/update_checkout/update_checkout/update_checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
SCRIPT_DIR = os.path.dirname(SCRIPT_FILE)


def child_init(lck):
global lock
lock = lck


def run_parallel(fn, pool_args, n_processes=0):
"""Function used to run a given closure in parallel.

Expand All @@ -37,17 +42,13 @@ def run_parallel(fn, pool_args, n_processes=0):
parallel implementation.
"""

def init(lck):
global lock
lock = lck

if n_processes == 0:
n_processes = cpu_count() * 2

lk = Lock()
print("Running ``%s`` with up to %d processes." %
(fn.__name__, n_processes))
pool = Pool(processes=n_processes, initializer=init, initargs=(lk,))
pool = Pool(processes=n_processes, initializer=child_init, initargs=(lk,))
results = pool.map_async(func=fn, iterable=pool_args).get(999999)
pool.close()
pool.join()
Expand Down