spark-instrumented-optimizer/python/pyspark/worker.py

53 lines
1.8 KiB
Python
Raw Normal View History

2012-08-10 04:10:02 -04:00
"""
Worker that receives input from Piped RDD.
"""
import sys
from base64 import standard_b64decode
# CloudPickler needs to be imported so that depicklers are registered using the
# copy_reg module.
2013-01-20 04:57:44 -05:00
from pyspark.accumulators import _accumulatorRegistry
2012-08-25 16:59:01 -04:00
from pyspark.broadcast import Broadcast, _broadcastRegistry
2012-08-19 20:12:51 -04:00
from pyspark.cloudpickle import CloudPickler
from pyspark.files import SparkFiles
2013-01-20 04:57:44 -05:00
from pyspark.serializers import write_with_length, read_with_length, write_int, \
read_long, read_int, dump_pickle, load_pickle, read_from_pickle_file
2012-08-10 04:10:02 -04:00
# Redirect stdout to stderr so that users must return values from functions.
old_stdout = sys.stdout
sys.stdout = sys.stderr
def load_obj():
return load_pickle(standard_b64decode(sys.stdin.readline().strip()))
2012-08-10 04:10:02 -04:00
def main():
split_index = read_int(sys.stdin)
spark_files_dir = load_pickle(read_with_length(sys.stdin))
SparkFiles._root_directory = spark_files_dir
SparkFiles._is_running_on_worker = True
sys.path.append(spark_files_dir)
num_broadcast_variables = read_int(sys.stdin)
2012-08-25 16:59:01 -04:00
for _ in range(num_broadcast_variables):
bid = read_long(sys.stdin)
value = read_with_length(sys.stdin)
_broadcastRegistry[bid] = Broadcast(bid, load_pickle(value))
func = load_obj()
bypassSerializer = load_obj()
if bypassSerializer:
dumps = lambda x: x
2012-08-10 04:10:02 -04:00
else:
dumps = dump_pickle
iterator = read_from_pickle_file(sys.stdin)
for obj in func(split_index, iterator):
write_with_length(dumps(obj), old_stdout)
2013-01-20 04:57:44 -05:00
# Mark the beginning of the accumulators section of the output
write_int(-1, old_stdout)
for aid, accum in _accumulatorRegistry.items():
write_with_length(dump_pickle((aid, accum._value)), old_stdout)
2012-08-10 04:10:02 -04:00
if __name__ == '__main__':
main()