You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@spark.apache.org by Timothee Besset <tt...@ttimo.net> on 2014/02/07 23:14:21 UTC

pyspark + gevent

I am exploring how to serve a large amount of requests against the same
dataset with a spark cluster.

I am only vaguely familiar with Java, but I'm a season Python programmer so
I went with pyspark. So far all my experiments are on a Spark 0.9, Python
2.7, MacOS X local cluster.

I was initially hoping to access the RRD from multiple SparkContext, but my
reading through the documentation and archives suggests that this is non
trivial, and the 'solutions' to do it are only given in Java at this point.

So instead I am looking at processing multiple calls through a single
SparkContext. The preferred method in Java is to access the SparkContext
from multiple threads.

However Python's dirty secret is that true concurrent threading doesn't
really work because of the GIL [1], and the canonical method for this class
of problems is to use 'green threads' (or cooperative light threads, fibers
etc.).

gevent [2] is the go-to solution for green threads on Python. It works by
'monkey patching' things like sockets, threads etc. from under pyspark's
own code to provide the 'cooperative multithreading'.

TL;DR - It works, with some minor quirks:

- If you do the following at the top of your script:

import gevent
import gevent.monkey
gevent.monkey.patch_all()
import pyspark

Then, when you create the SparkContext, the EchoOutputThread class in
pyspark.launch_gateway will hang on 'line = self.stream.readline()'

If you hit Ctrl+C twice at that point, execution will continue and work
fine.
This may be a problem due to local cluster etc. .. or something fairly easy
to fix. I haven't investigated yet.

- If you monkey patch everything except threads:

import gevent
import gevent.monkey
gevent.monkey.patch_all( thread = False )
import pyspark

Then you don't get stuck anymore .. but after a few seconds of running, one
of your greenlets will die with the following:

Traceback (most recent call last):
  File
"/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/gevent/hub.py",
line 534, in switch
    assert getcurrent() is self.hub, "Can only use Waiter.switch method
from the Hub greenlet"
AssertionError: Can only use Waiter.switch method from the Hub greenlet <io
at 0x10c8e1c50 fd=19 events=READ active callback=<bound method
Waiter.switch of <gevent.hub.Waiter object at 0x10dc1dcd0>> args=(<object
object at 0x10c029970>,)> failed with AssertionError

So, reason I'm posting this:

- If someone else is looking to use gevent with pyspark, I hope this will
give them a good basis to work from.
- I would like to make sure that gevent and Spark compatibility improves,
rather than degrades, so if there is something I can do to help make sure
this is the case, let me know.

Over the next week or two I plan to continue exploring this setup. Even if
I max out a single context, I'm pretty sure I will need to go to multiple
contexts at some point, so any feedback welcome on those earlier points.

TTimo

[1] http://en.wikipedia.org/wiki/Global_Interpreter_Lock
[2] http://www.gevent.org/