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+
18class QueryBuffer :
29 def __init__ (self , graphname , client , config ):
310 self .nodes = None
@@ -30,7 +37,9 @@ def __init__(self, graphname, client, config):
3037 self .nodes_created = 0 # Total number of nodes created
3138 self .relations_created = 0 # Total number of relations created
3239
33- # TODO consider using a queue to send commands asynchronously
40+ self .pool = Pool (nodes = 1 )
41+ self .tasks = []
42+
3443 def send_buffer (self ):
3544 """Send all pending inserts to Redis"""
3645 # Do nothing if we have no entities
@@ -43,10 +52,8 @@ def send_buffer(self):
4352 args .insert (0 , "BEGIN" )
4453 self .initial_query = False
4554
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 )
5057
5158 self .clear_buffer ()
5259
@@ -59,6 +66,23 @@ def clear_buffer(self):
5966 self .buffer_size = 0
6067 self .node_count = 0
6168 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 ])
6286
6387 def report_completion (self , runtime ):
6488 print ("Construction of graph '%s' complete: %d nodes created, %d relations created in %f seconds"
0 commit comments