Trying to apply udf on a large csv file that makes a model prediction based on some conditions, but for some reason the model is being loaded multiple number of times. Below is a sample snippet of how the flow looks like:
# main.py loads predict.py
from predict import predict_udf
data = spark.read("csv_path")
data.show()
| Column1 | Column2 |
| -------- | ------- |
| Class1 | |
| Class2 | |
data.withColumn("Column2",predict_udf(col("Column1"))
# predict.py
model = load_model() # Initialising model object
def predict(class_name):
if class_name == "Class1":
# Do something
elif class_name == "Class2":
# Do something else
predict_udf = udf(predict, StringType())
Ideally I was expecting the model to be loaded once since it is defined as a global variable, but the model is being loaded multiple number of times.