Skip to content

Instantly share code, notes, and snippets.

@ADCDS
Last active April 25, 2017 12:56
Show Gist options
  • Save ADCDS/63f9f714af569dbd123df3591fe559df to your computer and use it in GitHub Desktop.
Save ADCDS/63f9f714af569dbd123df3591fe559df to your computer and use it in GitHub Desktop.
Dijkstar python package: Shared memory graph
# https://pypi.python.org/pypi/Dijkstar
from dijkstar import Graph, find_path
from multiprocessing import Process, Manager
import collections
manager = Manager()
class GraphProxy(Graph):
def __init__(self, data=None):
# replaces default _data and _incoming attributes to a equivalent object (dict), but in shared memory
self._data = manager.dict()
self._incoming = collections.defaultdict(manager.dict)
if data is not None:
self.update(data)
class GraphHolder(object):
def __init__(self):
self.graph = GraphProxy()
self.graph.add_edge(1, 2, 10)
self.graph.add_edge(2, 3, 50)
class Processer(Process):
def __init__(self, graphHolder):
Process.__init__(self)
self.graphHolder = graphHolder
def run(self):
while True:
result = find_path(self.graphHolder.graph, 1, 3)
print(result)
myGraph = GraphHolder()
myProcesser = Processer(myGraph)
myProcesser.start()
# Main process should be still running when myProcessor's process start
# ref: https://stackoverflow.com/questions/25455462/share-list-between-process-in-python-server/25456494#25456494
while True:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment