Skip to content

Commit 3b498e1

Browse files
Ken Takagiwagiwa
Ken Takagiwa
authored andcommitted
Kill py4j callback server properly
1 parent 84a9668 commit 3b498e1

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

python/pyspark/streaming/context.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
# limitations under the License.
1616
#
1717

18-
import time
18+
import sys
19+
from signal import signal, SIGTERM, SIGINT
1920

2021
from pyspark.conf import SparkConf
2122
from pyspark.files import SparkFiles
@@ -63,22 +64,31 @@ def __init__(self, master=None, appName=None, sparkHome=None, pyFiles=None,
6364
6465
"""
6566

66-
# launch call back server
67-
if not gateway:
68-
gateway = launch_gateway()
69-
# gateway.restart_callback_server()
70-
7167
# Create the Python Sparkcontext
7268
self._sc = SparkContext(master=master, appName=appName, sparkHome=sparkHome,
7369
pyFiles=pyFiles, environment=environment, batchSize=batchSize,
7470
serializer=serializer, conf=conf, gateway=gateway)
71+
72+
# Start py4j callback server
73+
SparkContext._gateway.restart_callback_server()
74+
self._clean_up_trigger()
7575
self._jvm = self._sc._jvm
7676
self._jssc = self._initialize_context(self._sc._jsc, duration._jduration)
7777

7878
# Initialize StremaingContext in function to allow subclass specific initialization
7979
def _initialize_context(self, jspark_context, jduration):
8080
return self._jvm.JavaStreamingContext(jspark_context, jduration)
8181

82+
def _clean_up_trigger(self):
83+
"""Kill py4j callback server properly using signal lib"""
84+
85+
def clean_up_handler(*args):
86+
SparkContext._gateway.shutdown()
87+
sys.exit(0)
88+
89+
for sig in (SIGINT, SIGTERM):
90+
signal(sig, clean_up_handler)
91+
8292
def start(self):
8393
"""
8494
Start the execution of the streams.

0 commit comments

Comments
 (0)