Last active
April 25, 2017 12:56
-
-
Save ADCDS/63f9f714af569dbd123df3591fe559df to your computer and use it in GitHub Desktop.
Dijkstar python package: Shared memory graph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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