Skip to content

Commit aa8bb69

Browse files
committed
Make update-checkout work with Python3
Python3 doesn't like to pickle local functions, so make the multiprocessing initialization function be global. Python3 multiprocessing also seems to require conditional execution of the top-level code. TBH, I'm not entirely sure why Python 2 didn't need this.
1 parent e32462f commit aa8bb69

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

utils/update-checkout

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
#!/usr/bin/env python
22

33
import sys
4-
54
import update_checkout
65

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

utils/update_checkout/update_checkout/update_checkout.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
SCRIPT_FILE = os.path.abspath(__file__)
2929
SCRIPT_DIR = os.path.dirname(SCRIPT_FILE)
3030

31+
def child_init(lck):
32+
global lock
33+
lock = lck
3134

3235
def run_parallel(fn, pool_args, n_processes=0):
3336
"""Function used to run a given closure in parallel.
@@ -37,17 +40,13 @@ def run_parallel(fn, pool_args, n_processes=0):
3740
parallel implementation.
3841
"""
3942

40-
def init(lck):
41-
global lock
42-
lock = lck
43-
4443
if n_processes == 0:
4544
n_processes = cpu_count() * 2
4645

4746
lk = Lock()
4847
print("Running ``%s`` with up to %d processes." %
4948
(fn.__name__, n_processes))
50-
pool = Pool(processes=n_processes, initializer=init, initargs=(lk,))
49+
pool = Pool(processes=n_processes, initializer=child_init, initargs=(lk,))
5150
results = pool.map_async(func=fn, iterable=pool_args).get(999999)
5251
pool.close()
5352
pool.join()

0 commit comments

Comments
 (0)