Skip to content

Commit

Permalink
f in foreach could be generator or not.
Browse files Browse the repository at this point in the history
  • Loading branch information
davies committed Aug 24, 2014
1 parent 2871b80 commit 1789cd4
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions python/pyspark/rdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,12 +655,14 @@ def foreachPartition(self, f):
>>> def f(iterator):
... for x in iterator:
... print x
... yield None
>>> sc.parallelize([1, 2, 3, 4, 5]).foreachPartition(f)
"""
def func(it):
f(it)
return iter([])
r = f(it)
try:
return iter(r)
except TypeError:
return iter([])
self.mapPartitions(func).count() # Force evaluation

def collect(self):
Expand Down

0 comments on commit 1789cd4

Please sign in to comment.