Erlang global PID - how to save it in MySQL -
problem : have multiple erlang services, each of them identified erlang pid. i'd save {pid, servicename} in database (mysql) in such way erlang process in cluster needs access service need select service name pid , gen_server call it.
- the pid should saved after converted "global" pid term_to_binary(self()) - basically, imagine erlang process on node1 saving <0.45.0> in db: means process b on node2 trying access service represented <0.45.0> not succeed since "<0.45.0>" valid on node1; saving pid = term_to_binary(self()) correctly decode global pid if run binary_to_term(pid) on node2 when read db
- the mysql query formatted "insert servicetable ... " ++ io_lib:format("servicepid = '~p', " [binary_to_list(term_to_binary(self()))]) ++
- when read pid db, <<"[131,103,100,0,23,115"...>> , problem : can convert binary <<131, 103, 100, etc ... >> ???
(i know workaround problem example registering services, etc - have reasons not that).
thank you.
based on show, guess avoid transformation binary list, io_lib:format transform binary string representation of it.
later can retrieve pid using erl_parse , erl_eval modules:
1> pid = self(). <0.32.0> 2> 2> string = lists:flatten(io_lib:format("~p.~n",[term_to_binary(pid)])). "<<131,103,100,0,13,110,111,110,111,100,101,64,110,111,104,111,115,116,0,0,0,32,\n 0,0,0,0,0>>.\n" 3> %% equivalent form of sql. 3> {done,{ok,t,_},[]} = erl_scan:tokens([],string,1). {done,{ok,[{'<<',1}, {integer,1,131}, {',',1}, {integer,1,103}, {',',1}, {integer,1,100}, {',',1}, {integer,1,0}, {',',1}, {integer,1,13}, {',',1}, {integer,1,110}, {',',1}, {integer,1,111}, {',',1}, {integer,1,110}, {',',1}, {integer,1,111}, {',',1}, {integer,1,100}, {',',1}, {integer,1,101}, {',',1}, {integer,...}, {...}|...], 3}, []} 4> {ok,tree} = erl_parse:parse_exprs(t). {ok,[{bin,1, [{bin_element,1,{integer,1,131},default,default}, {bin_element,1,{integer,1,103},default,default}, {bin_element,1,{integer,1,100},default,default}, {bin_element,1,{integer,1,0},default,default}, {bin_element,1,{integer,1,13},default,default}, {bin_element,1,{integer,1,110},default,default}, {bin_element,1,{integer,1,111},default,default}, {bin_element,1,{integer,1,110},default,default}, {bin_element,1,{integer,1,111},default,default}, {bin_element,1,{integer,1,100},default,default}, {bin_element,1,{integer,1,101},default,default}, {bin_element,1,{integer,1,64},default,default}, {bin_element,1,{integer,1,110},default,default}, {bin_element,1,{integer,1,111},default,default}, {bin_element,1,{integer,1,104},default,default}, {bin_element,1,{integer,1,111},default,default}, {bin_element,1,{integer,1,115},default,default}, {bin_element,1,{integer,1,...},default,default}, {bin_element,1,{integer,...},default,...}, {bin_element,1,{...},...}, {bin_element,1,...}, {bin_element,...}, {...}|...]}]} 5> {value,res,[]} = erl_eval:exprs(tree,[]). {value,<<131,103,100,0,13,110,111,110,111,100,101,64,110, 111,104,111,115,116,0,0,0,32,0,0,0,0,0>>, []} 6> pid = binary_to_term(res). %% that's :o) <0.32.0>
Comments
Post a Comment