I believe your problem is a problem with Python. If you try the following non-Spark Python code, similarly the error with "V" is undefined ":
def runner(func):
func()
def main():
V = 22
A = runner(worker)
def worker():
print V
if __name__ == '__main__':
main()
One fix: you can move worker()inward main()(or, alternatively, make a Vglobal variable):
def main():
sc = SparkContext()
someValue = rand()
V = sc.broadcast(someValue)
def worker(element):
element *= V.value
A = sc.parallelize().map(worker)