Skip to content

Commit 630f617

Browse files
authored
Use vertices SparkSession (#474)
Tested on Java 8 and 11, passes all checks. Merging. * Update graphframe.py * Use vertices `SparkSession` instead of the thread's * Add unittest for multithreading usage
1 parent 06fd2ae commit 630f617

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎python/graphframes/graphframe.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class GraphFrame:
6464
def __init__(self, v: DataFrame, e: DataFrame) -> None:
6565
self._vertices = v
6666
self._edges = e
67-
self._spark = SparkSession.getActiveSession()
67+
self._spark = v.sparkSession
6868
self._sc = self._spark._sc
6969
self._jvm_gf_api = _java_api(self._sc)
7070

‎python/graphframes/tests.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,28 @@ def test_triangle_counts(self):
409409
c = g.triangleCount()
410410
for row in c.select("id", "count").collect():
411411
self.assertEqual(row.asDict()['count'], 1)
412+
413+
def test_mutithreaded_sparksession_usage(self):
414+
# Test that we can use the GraphFrame API from multiple threads
415+
localVertices = [(1, "A"), (2, "B"), (3, "C")]
416+
localEdges = [(1, 2, "love"), (2, 1, "hate"), (2, 3, "follow")]
417+
v = self.spark.createDataFrame(localVertices, ["id", "name"])
418+
e = self.spark.createDataFrame(localEdges, ["src", "dst", "action"])
419+
420+
421+
exc = None
422+
def run_graphframe() -> None:
423+
try:
424+
GraphFrame(v, e)
425+
except Exception as _e:
426+
nonlocal exc
427+
exc = _e
428+
429+
import threading
430+
thread = threading.Thread(target=run_graphframe)
431+
thread.start()
432+
thread.join()
433+
self.assertIsNone(exc, f"Exception was raised in thread: {exc}")
412434

413435

414436
class GraphFrameExamplesTest(GraphFrameTestCase):

0 commit comments

Comments
 (0)