Skip to content

Commit ccaef6d

Browse files
committed
Merge pull request #580 from enthought/refactor/nonce-and-basename
Refactoring: nonce() and DISTARRAY_BASE_NAME
2 parents ceb54b3 + dc17f97 commit ccaef6d

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

distarray/globalapi/context.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@
2121
import numpy
2222

2323
from distarray.externals import six
24+
from distarray import DISTARRAY_BASE_NAME
2425
from distarray.globalapi import ipython_cleanup
2526
from distarray.globalapi.distarray import DistArray
2627
from distarray.globalapi.maps import Distribution, asdistribution
2728

2829
from distarray.globalapi.ipython_utils import IPythonClient
29-
from distarray.utils import uid, DISTARRAY_BASE_NAME, has_exactly_one
30+
from distarray.utils import uid, nonce, has_exactly_one
3031
from distarray.localapi.proxyize import Proxy
3132

3233
# mpi context
@@ -815,7 +816,7 @@ def func_wrapper(func, apply_nonce, context_key, args, kwargs, autoproxyize):
815816
# default arguments
816817
args = () if args is None else args
817818
kwargs = {} if kwargs is None else kwargs
818-
apply_nonce = uid()[13:]
819+
apply_nonce = nonce()
819820
wrapped_args = (func, apply_nonce, self.context_key, args, kwargs, autoproxyize)
820821

821822
targets = self.targets if targets is None else targets
@@ -973,7 +974,7 @@ def apply(self, func, args=None, kwargs=None, targets=None, autoproxyize=False):
973974
kwargs = {} if kwargs is None else kwargs
974975
targets = self.targets if targets is None else targets
975976

976-
apply_nonce = uid()[13:]
977+
apply_nonce = nonce()
977978
apply_metadata = (apply_nonce, self.context_key)
978979

979980
if not isinstance(func, types.BuiltinFunctionType):

distarray/localapi/proxyize.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ def cleanup(self):
3030

3131
class Proxyize(object):
3232

33+
"""Callable that, given an object, returns a Proxy object.
34+
35+
You must call `set_state` on the instance before you can "call" it.
36+
"""
37+
3338
def __init__(self):
3439
self.count = None
3540
self.state = None
@@ -39,6 +44,7 @@ def set_state(self, state):
3944
self.count = 0
4045

4146
def str_counter(self):
47+
"""Return the str value of `self.count`, then increment its value."""
4248
res = str(self.count)
4349
self.count += 1
4450
return res
@@ -50,5 +56,6 @@ def next_name(self):
5056
return DISTARRAY_BASE_NAME + self.state + self.str_counter()
5157

5258
def __call__(self, obj):
59+
"""Return a `Proxy` object given an object `obj`."""
5360
new_name = self.next_name()
5461
return Proxy(new_name, obj, '__main__')

distarray/mpi_engine.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ def __init__(self):
3434

3535
# make engines intracomm (Context._base_comm):
3636
Engine.INTERCOMM = initial_comm_setup()
37-
assert self.world.rank != 0
37+
assert self.world.rank != self.client_rank
3838
while True:
39-
msg = Engine.INTERCOMM.recv(source=0)
39+
msg = Engine.INTERCOMM.recv(source=self.client_rank)
4040
val = self.parse_msg(msg)
4141
if val == 'kill':
4242
break

distarray/utils.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
import random
1414
import uuid
1515

16+
from distarray import DISTARRAY_BASE_NAME
1617
from distarray.externals.six import next
1718

18-
DISTARRAY_BASE_NAME = '__distarray__'
19+
1920
DISTARRAY_RANDOM = random.Random()
2021

2122

@@ -31,10 +32,13 @@ def distarray_random_getstate():
3132
return DISTARRAY_RANDOM.getstate()
3233

3334

35+
def nonce():
36+
return uuid.UUID(int=DISTARRAY_RANDOM.getrandbits(8 * 16)).hex[:16]
37+
38+
3439
def uid():
35-
""" Get a unique name for a distarray object. """
36-
suffix = uuid.UUID(int=DISTARRAY_RANDOM.getrandbits(8 * 16)).hex[:16]
37-
return DISTARRAY_BASE_NAME + suffix
40+
"""Get a unique name for a distarray object. """
41+
return DISTARRAY_BASE_NAME + nonce()
3842

3943

4044
def multi_for(iterables):

0 commit comments

Comments
 (0)