1
+ from pathos .pools import ThreadPool as Pool
2
+
3
+ def run (client , graphname , args ):
4
+ result = client .execute_command ("GRAPH.BULK" , graphname , * args )
5
+ stats = result .split (', ' .encode ())
6
+ return stats
7
+
1
8
class QueryBuffer :
2
9
def __init__ (self , graphname , client , config ):
3
10
self .nodes = None
@@ -30,7 +37,9 @@ def __init__(self, graphname, client, config):
30
37
self .nodes_created = 0 # Total number of nodes created
31
38
self .relations_created = 0 # Total number of relations created
32
39
33
- # TODO consider using a queue to send commands asynchronously
40
+ self .pool = Pool (nodes = 1 )
41
+ self .tasks = []
42
+
34
43
def send_buffer (self ):
35
44
"""Send all pending inserts to Redis"""
36
45
# Do nothing if we have no entities
@@ -43,10 +52,8 @@ def send_buffer(self):
43
52
args .insert (0 , "BEGIN" )
44
53
self .initial_query = False
45
54
46
- result = self .client .execute_command ("GRAPH.BULK" , self .graphname , * args )
47
- stats = result .split (', ' .encode ())
48
- self .nodes_created += int (stats [0 ].split (' ' .encode ())[0 ])
49
- self .relations_created += int (stats [1 ].split (' ' .encode ())[0 ])
55
+ task = self .pool .apipe (run , self .client , self .graphname , args )
56
+ self .add_task (task )
50
57
51
58
self .clear_buffer ()
52
59
@@ -59,6 +66,23 @@ def clear_buffer(self):
59
66
self .buffer_size = 0
60
67
self .node_count = 0
61
68
self .relation_count = 0
69
+
70
+ def add_task (self , task ):
71
+ self .tasks .append (task )
72
+ if len (self .tasks ) == 5 :
73
+ task = self .tasks .pop (0 )
74
+ stats = task .get ()
75
+ self .update_stats (stats )
76
+
77
+ def wait_pool (self ):
78
+ for task in self .tasks :
79
+ stats = task .get ()
80
+ self .update_stats (stats )
81
+ self .tasks .clear ()
82
+
83
+ def update_stats (self , stats ):
84
+ self .nodes_created += int (stats [0 ].split (' ' .encode ())[0 ])
85
+ self .relations_created += int (stats [1 ].split (' ' .encode ())[0 ])
62
86
63
87
def report_completion (self , runtime ):
64
88
print ("Construction of graph '%s' complete: %d nodes created, %d relations created in %f seconds"
0 commit comments