Skip to content
Snippets Groups Projects
Commit 898cb652 authored by Jeff Zhang's avatar Jeff Zhang Committed by Davies Liu
Browse files

[SPARK-15803] [PYSPARK] Support with statement syntax for SparkSession

## What changes were proposed in this pull request?

Support with statement syntax for SparkSession in pyspark

## How was this patch tested?

Manually verify it. Although I can add unit test for it, it would affect other unit test because the SparkContext is stopped after the with statement.

Author: Jeff Zhang <zjffdu@apache.org>

Closes #13541 from zjffdu/SPARK-15803.
parent 4c64e88d
No related branches found
No related tags found
No related merge requests found
...@@ -581,6 +581,22 @@ class SparkSession(object): ...@@ -581,6 +581,22 @@ class SparkSession(object):
""" """
self._sc.stop() self._sc.stop()
@since(2.0)
def __enter__(self):
"""
Enable 'with SparkSession.builder.(...).getOrCreate() as session: app' syntax.
"""
return self
@since(2.0)
def __exit__(self, exc_type, exc_val, exc_tb):
"""
Enable 'with SparkSession.builder.(...).getOrCreate() as session: app' syntax.
Specifically stop the SparkSession on exit of the with block.
"""
self.stop()
def _test(): def _test():
import os import os
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment