Skip to content
Closed
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
8 changes: 8 additions & 0 deletions python/pyspark/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,14 @@ def getStart(split):
return start0 + int((split * size / numSlices)) * step

def f(split, iterator):
# it's an empty iterator here but we need this line for triggering the
# logic of signal handling in FramedSerializer.load_stream, for instance,
# SpecialLengths.END_OF_DATA_SECTION in _read_with_length. Since
# FramedSerializer.load_stream produces a generator, the control should
# at least be in that function once. Here we do it by explicitly converting
# the empty iterator to a list, thus make sure worker reuse takes effect.
# See more details in SPARK-26549.
assert len(list(iterator)) == 0
return xrange(getStart(split), getStart(split + 1), step)

return self.parallelize([], numSlices).mapPartitionsWithIndex(f)
Expand Down
12 changes: 11 additions & 1 deletion python/pyspark/tests/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from py4j.protocol import Py4JJavaError

from pyspark.testing.utils import ReusedPySparkTestCase, QuietTest
from pyspark.testing.utils import ReusedPySparkTestCase, PySparkTestCase, QuietTest

if sys.version_info[0] >= 3:
xrange = range
Expand Down Expand Up @@ -145,6 +145,16 @@ def test_with_different_versions_of_python(self):
self.sc.pythonVer = version


class WorkerReuseTest(PySparkTestCase):

def test_reuse_worker_of_parallelize_xrange(self):
rdd = self.sc.parallelize(xrange(20), 8)
previous_pids = rdd.map(lambda x: os.getpid()).collect()
current_pids = rdd.map(lambda x: os.getpid()).collect()
for pid in current_pids:
self.assertTrue(pid in previous_pids)


if __name__ == "__main__":
import unittest
from pyspark.tests.test_worker import *
Expand Down