Github user JoshRosen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/1977#discussion_r28013345
  
    --- Diff: python/pyspark/shuffle.py ---
    @@ -529,6 +520,295 @@ def sorted(self, iterator, key=None, reverse=False):
             return heapq.merge(chunks, key=key, reverse=reverse)
     
     
    +class ExternalList(object):
    +    """
    +    ExternalList can have many items which cannot be hold in memory in
    +    the same time.
    +
    +    >>> l = ExternalList(range(100))
    +    >>> len(l)
    +    100
    +    >>> l.append(10)
    +    >>> len(l)
    +    101
    +    >>> for i in range(10240):
    +    ...     l.append(i)
    +    >>> len(l)
    +    10341
    +    >>> import pickle
    +    >>> l2 = pickle.loads(pickle.dumps(l))
    +    >>> len(l2)
    +    10341
    +    >>> list(l2)[100]
    +    10
    +    """
    +    LIMIT = 10240
    +
    +    def __init__(self, values):
    +        self.values = values
    +        self.disk_count = 0
    +        self._file = None
    +        self._ser = None
    +
    +    def __getstate__(self):
    +        if self._file is not None:
    +            self._file.flush()
    +            f = os.fdopen(os.dup(self._file.fileno()))
    +            f.seek(0)
    +            bytes = f.read()
    +        else:
    +            bytes = ''
    +        return self.values, self.disk_count, bytes
    +
    +    def __setstate__(self, item):
    +        self.values, self.disk_count, bytes = item
    +        if bytes:
    +            self._open_file()
    +            self._file.write(bytes)
    +        else:
    +            self._file = None
    +            self._ser = None
    +
    +    def __iter__(self):
    +        if self._file is not None:
    +            self._file.flush()
    +            # read all items from disks first
    +            with os.fdopen(os.dup(self._file.fileno()), 'r') as f:
    +                f.seek(0)
    +                for values in self._ser.load_stream(f):
    +                    for v in values:
    +                        yield v
    +
    +        for v in self.values:
    +            yield v
    +
    +    def __len__(self):
    +        return self.disk_count + len(self.values)
    +
    +    def append(self, value):
    +        self.values.append(value)
    +        # dump them into disk if the key is huge
    +        if len(self.values) >= self.LIMIT:
    +            self._spill()
    +
    +    def _open_file(self):
    +        dirs = _get_local_dirs("objects")
    +        d = dirs[id(self) % len(dirs)]
    +        if not os.path.exists(d):
    +            os.makedirs(d)
    +        p = os.path.join(d, str(id))
    +        self._file = open(p, "w+", 65536)
    +        self._ser = CompressedSerializer(PickleSerializer())
    +        os.unlink(p)
    --- End diff --
    
    Ah, I see.  From the 
[unlink](http://pubs.opengroup.org/onlinepubs/009695399/functions/unlink.html) 
spec:
    
    > When the file's link count becomes 0 and no process has the file open, 
the space occupied by the file shall be freed and the file shall no longer be 
accessible. If one or more processes have the file open when the last link is 
removed, the link shall be removed before unlink() returns, but the removal of 
the file contents shall be postponed until all references to the file are 
closed.
    
    This is a very clever trick!


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to