I want to take noPairs and noThreads as a input from user. For example calculate shortest path between 4 pairs using only 2 threads
. But in my code for every pair multiple threads are created. Instead i want to create given number of threads and then perform operations using them. Can someone help me.
My code :
import threading
from py2neo import neo4j
from random import randint
#------------------------------------------------------------------------------
TOTAL = 0
MY_LOCK = threading.Lock()
class TestJVM(threading.Thread):
def __init__(self,s_node,e_node):
"""
Default constructor
"""
self.graph_db = neo4j.GraphDatabaseService("http://localhost:7474/db/data/")
self.query_string = """START beginning=node(%s), end=node(%s)
MATCH p = shortestPath(beginning-[*]-end)
RETURN p"""%(s_node,e_node) # Pass start and end node values
threading.Thread.__init__(self)
def run(self):
"""
Run method
"""
MY_LOCK.acquire()
result = neo4j.CypherQuery(self.graph_db,self.query_string).execute()
for r in result:
print(r.p) # p is a py2neo.neo4j.Path object
MY_LOCK.release()
if __name__ == "__main__":
try:
noThreads = int(input("Enter Number of Threads : "))
noPairs = int(input("Enter Number of Pairs : ")) # Take number of pairs as a input
for i in range(0,noPairs):
start_node = randint(0,20); # Enter range for generating random numbers
end_node = randint(0,20); # Enter range for generating random numbers
print("start node is : %s and end node is : %s"%(start_node,end_node) )
for t in range(0,noThreads):
a = TestJVM(start_node,end_node)
a.start()
except Exception as e:
print(e)