Wednesday 15 January 2014

actor - Can't send anything to spawned Erlang process -



actor - Can't send anything to spawned Erlang process -

i have next erlang code:

#!/usr/bin/env escript %%! -pz ../deps/amqp_client ../deps/rabbit_common ../deps/amqp_client/ebin ../deps/rabbit_common/ebin % rmq module -module(rmq). -export([main/1, send/1, validate/0, test/0]). -include_lib("../deps/amqp_client/include/amqp_client.hrl"). main(_) -> %send(<<"test_esio">>), %validate(), pid = spawn(rmq, test, []), % pid = spawn(fun() -> test() end), <= i've tried way pid ! s. test() -> receive s -> io:format("bar ~n"), send(<<"esio">>), test(); -> validate(), test(); _ -> io:format("foo"), test() end.

i run with: excript rmq.erl

this code doesn't work. looks spawn doesn't work.

rest of code works, function send , validate works correctly if run main (i've commented its). i'm doing wrong?

sorry, maybe it's dumb question i'm beginner erlang. i've tried search reply in net , books , failed...

the problem not in spawn, in module/escript confusion.

in few words, escript file not modules, not point of erlang vm, if utilize -module() directive. interpreted, , not compiled @ all, , can not called module "rmq:test()", or in case trough dynamic module phone call spawn.

easiest solution separate script actual modules. in rmq.es start proper module:

#!/usr/bin/env escript %%! -pz ../deps/amqp_client ../deps/rabbit_common ../deps/amqp_client/ebin ../deps/rabbit_common/ebin main(_) -> rmq:start().

and there in module rmq.erl:

-module(rmq). -export([start/0, send/1, validate/0, test/0]). -include_lib("../deps/amqp_client/include/amqp_client.hrl"). start() -> pid = spawn(rmq, test, []), %% pid = spawn(?module, test, []), %% works macro %% pid = spawn(fun() -> test() end), <= i've tried way pid ! s. test() -> receive s -> io:format("bar ~n"), send(<<"esio">>), test(); -> validate(), test(); _ -> io:format("foo"), test() end.

or start module without escript, -run flag this

erl -pz deps/*/ebin -run rmq start

edit regarding compilation problems

you compile modules erlc command. compile utilize erlc rmq.erl, produce rmq.beam file in current directory. convention maintain source files in src directory, compiled files in ebin direcory, , things run-scripts placed in top directory. that:

project |-- ebin | |-- rmq.beam | |-- src | |-- rmq.erl | |-- rmq.es

assuming run shell commands form project directory, compile file src , place .beam binaries in ebin utilize erlc src/rmq.erl -o ebin, or erlc src/* -o ebin in documentation can find explanation of -o flag"

-o directory

the directory compiler should place output files. if not specified, output files placed in current working directory.

then, after compilation can run code, either erl erlang vm or using escript (which kind-off uses erl.

erl runs code compiled modules, , needs able locate compiled *.ebin binaries. uses code path, list of directors in search files. list automatically consist standard library directories, , of course of study can add together directories own code utilize of -pa flag.

-pa dir1 dir2 ...

adds specified directories origin of code path, similar code:add_pathsa/1. see code(3). alternative -pa, if several directories prepended code , directories have mutual parent directory, parent directory specified in erl_libs environment variable. see code(3).

in case erl -pa ebin, or include binaries of deps can utilize erl -pa ebin -pa deps/*/ebin.

exactly same options used in sec line of escript. exception of * character, not expand in shell. in escript have provide paths each dependency separately. thought of including -pa ebin stays same.

to automate , standardize process tools rebar , erlang.mk created (i recommend later). using should help little workflow.

erlang actor erlang-escript

No comments:

Post a Comment