[英]Do I need to stop spark after creating sparksession using pyspark?
So I have this:所以我有这个:
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName("NewSpark").getOrCreate()
I heard you need to stop spark once you're done but is this necessary in my case since it's just a python program?我听说你完成后需要停止 spark,但在我的情况下这是必要的,因为它只是一个 python 程序?
The session will be closed if the spark
object gets destroyed or if the script exits.如果
spark
对象被破坏或脚本退出,会话将关闭。 So you shouldn't need to worry about "dangling connections" or anything like that.因此,您无需担心“悬空连接”或类似问题。
However, if you have a bunch of non-spark work that you want to do at the end of the script, it may still be a good idea to stop the session early to avoid holding that connection open.但是,如果您想在脚本结束时执行大量非 Spark 工作,那么提前停止会话以避免保持该连接打开可能仍然是一个好主意。
Note that you can use the SparkSession
object as a context manager to automatically stop it at the end of a scope:请注意,您可以使用
SparkSession
对象作为上下文管理器在范围结束时自动停止它:
with SparkSession.builder.appName("NewSpark").getOrCreate() as spark:
# do stuff
# ...
# spark.stop() gets called automatically here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.