如何将图传递给不同的进程和节点? [英] How to pass a digraph to a different process and node?

查看:155
本文介绍了如何将图传递给不同的进程和节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个 有向图 过程A中的术语,我想将这个图传递给另一个节点上的进程。每当我在另一个进程中使用这个图表时,我收到错误,如:

I have created a digraph term on process A and I want to pass this digraph to a process on another node. Whenever I use this digraph on the other process I am getting errors such as:

** {badarg,
   [{ets,insert,[598105,{"EPqzYxiM9UV0pplPTRg8vX28h",[]}],[]},
    {digraph,do_add_vertex,2,[{file,"digraph.erl"},{line,377}]},

成为一个有向图是基于ETS,似乎这是相当复杂的,使一个图形几乎独立于其创建的过程。我发现这个条目显示了类似的问题:不同进程的ETS

Becasuse a digraph is based on ETS, it appears that this is quite more complicated, making a digraph pretty much standalone on the process it was created. I have found this entry that reveals a similar problem : ETS on a different process

我知道我可以在服务器中创建有向图,然后通过otp消息连接到它,但是我不能这样做在我的建筑。所有节点可以使用旨在通过状态的特定方法进行通信。

I know I can create the digraph in a server an then connect to it through otp messages, but I cannot do that in my architecture. All nodes can communicate using a specific approach designed to pass the state along as Terms.

在我看来,在不能直接相互通信的不同节点之间传送图表是不可能的。总的来说,似乎无法直接序列化有向图。我在想,我可以放松图作为顶点和边缘的列表,然后在另一个进程(不是高效,执行或优雅)传输和重新创建它。对序列化的更好方法的任何想法?有没有办法序列化ETS商店的图形状态?

It appears to me that having digraphs sent accross different nodes that cannot directly communicate with each other is not possible. Overall, it appears that a digraph cannot be directly serialized. I am thinking that I can "unwind" the digraph as a list of vertices and edges and then transmit and recreate it on the other process (not efficient, performing or elegant). Any thoughts on a better way to serialize it ? Is there a way to serialize the digraph state out of the ETS store ?

任何想法?

推荐答案

您可以序列化/反序列化这样的图形;

You can serialize/deserialize a digraph like this;

serialize({digraph, V, E, N, B}) ->
    {ets:tab2list(V),
     ets:tab2list(E),
     ets:tab2list(N),
     B}.

deserialize({VL, EL, NL, B}) ->       
    DG = {digraph, V, E, N, B} = case B of 
       true -> digraph:new();
       false -> digraph:new([acyclic])
    end,
    ets:delete_all_objects(V)
    ets:delete_all_objects(L)
    ets:delete_all_objects(N)
    ets:insert(V, VL)
    ets:insert(E, EL)
    ets:insert(N, NL)
    DG.

这是我用来测试的代码;

And this is the code I used to test this;

passer() ->
    G = digraph:new(),
    V1 = digraph:add_vertex(G),
    V2 = digraph:add_vertex(G),
    V3 = digraph:add_vertex(G),
    digraph:add_edge(G, V1, V2, "edge1"),
    digraph:add_edge(G, V1, V3, "edge2"),
    Pid = spawn(fun receiver/0),
    Pid ! serialize(G).

receiver() ->
     receive
         SG = {_VL, _EL, _NL, _B} ->
            G = deserialize(SG),
            io:format("Edges: ~p~n", [digraph:edges(G)]),
            io:format("Edges: ~p~n", [digraph:vertices(G)])
     end.

相当丑陋的解决方案,但工作。我认为这是节点之间传递图的唯一方法,因为ets表不能在节点之间共享。

Pretty ugly solution but works. I think this is the only way pass digraph between nodes since ets tables cannot be shared between nodes.

编辑:删除不必要的循环

remove unnecessary loops

这篇关于如何将图传递给不同的进程和节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆