I've installed GDL recently, since I don't have a license for IDL.
I was trying to run and IDL (.pro) script for simulate exoplanetary transit (with exomoon) and I got some issues:
GDL> .reset_session
(gdl:16530): Gtk-WARNING **: gtk_disable_setlocale() must be called before gtk_init()
GDL>
GDL> restore, 'paramk63.save', /v
% Procedure not found: RESTORE
% Execution halted at: $MAIN$
GDL>
GDL> Per = PER
GDL> ap = A
% Variable is undefined: A
% Execution halted at: $MAIN$
GDL> Rplan = RP
% Variable is undefined: RP
% Execution halted at: $MAIN$
GDL> inc = INC
GDL> Pm = 1
GDL> Rmoon = 0.03
GDL> dmoon = 4.0
GDL> tetam0 = 0.0
GDL> dt = 1.0
GDL> wl = WL
GDL>
GDL> eclipse_moon,Per,ap,Rplan,inc,Pm,Rmoon,dmoon,tetam0,dt,wl,/plot
% Compiled module: ECLIPSE_MOON.
% ECLIPSE_MOON: Ambiguous: Variable is undefined: MEAN or: Function not found: MEAN
% Execution halted at: ECLIPSE_MOON 23 /home/fabian/Downloads/eclipse_moon.pro
% $MAIN$
GDL> 0:45 / 9:10
% ECLIPSE_MOON: Parser syntax error: unexpected token: 0
GDL>
I ran this same code on linux with IDL installed and it was perfectly fine.
It seems I have to install some more stuff. Is that right?
Looks like it couldn't find a function called MEAN. Make sure it is in your !path and that you are calling it properly. When IDL (or GDL) runs a function or program, it searches all of the directories in the variable !path to find it. If it is found, IDL will load the function and continue compiling. In this case I expect that the function ECLIPSE_MOON calls MEAN on line 23. But MEAN (which is usually built in in IDL) is not found in GDL. So you need to make sure it can be found.
To check what your path, in a GDL session type print,!path and make sure the function MEAN is in one of those directories.
I've tried to get the Universal Recommender template set up as per the instructions located at UR Quickstart. Getting an import error. Is there a dependency/step that I missed?
[ERROR] [Engine$] [error] \import org.apache.mahout.math.cf.{DownsamplableCrossOccurrenceDataset, SimilarityAnalysis}
[ERROR] [Engine$] [error] ^
[ERROR] [Engine$] [error] one error found
The relevant block in my build.sbt file is as follows:
libraryDependencies ++= Seq(
"org.apache.predictionio" %% "apache-predictionio-core" % pioVersion % "provided",
"org.apache.predictionio" %% "apache-predictionio-data-elasticsearch1" % pioVersion % "provided",
"org.apache.spark" %% "spark-core" % "1.4.0" % "provided",
"org.apache.spark" %% "spark-mllib" % "1.4.0" % "provided",
"org.xerial.snappy" % "snappy-java" % "1.1.1.7",
// Mahout's Spark libs
"org.apache.mahout" %% "mahout-math-scala" % mahoutVersion,
"org.apache.mahout" %% "mahout-spark" % mahoutVersion
exclude("org.apache.spark", "spark-core_2.10"),
"org.apache.mahout" % "mahout-math" % mahoutVersion,
"org.apache.mahout" % "mahout-hdfs" % mahoutVersion
exclude("com.thoughtworks.xstream", "xstream")
exclude("org.apache.hadoop", "hadoop-client"),
//"org.apache.hbase" % "hbase-client" % "0.98.5-hadoop2" %
"provided",
Please disregard. I'm not familiar with scala syntax so I didn't realize the incorrect inclusion of the backslash in the source file was problematic.
Consider this answer as an alternative for build PIO-UR engine.
Actually, I also had struggled with these dependency issues. What I am telling you is not the right solution for this. But this will provide you a working PredictionIO UR engine.
Use the docker image for the universal recommender template.
Use this link for getting PIO-UR docker image.
If you are not familiar with docker, use these links below :
INSTALLATIONS:
Docker for MAC
Docker for Windows
For Ubuntu, use automated script: curl -sSL https://get.docker.com/ | sh
Then use the above image from git for using the UR template. By using docker, we don't need to struggle with the dependencies. The readme file in the git repository is really helpful and use that as a guide. You can set the PIO engine up and run with 3 simple commands.
I'm working on a Erlang. How can I pass command line parameters to it?
Program File-
-module(program).
-export([main/0]).
main() ->
io:fwrite("Hello, world!\n").
Compilation Command:
erlc Program.erl
Execution Command-
erl -noshell -s program main -s init stop
I need to pass arguments through execution command and want to access them inside main written in program's main.
$ cat program.erl
-module(program).
-export([main/1]).
main(Args) ->
io:format("Args: ~p\n", [Args]).
$ erlc program.erl
$ erl -noshell -s program main foo bar -s init stop
Args: [foo,bar]
$ erl -noshell -run program main foo bar -s init stop
Args: ["foo","bar"]
It is documented in erl man page.
I would recommend using escript for this purpose because it has a simpler invocation.
These are not really commandline-parameters, but if you want to use environment-variables, the os-module might help. os:getenv() gives you a list of all environment variables. os:getenv(Var) gives you the value of the variable as a string, or returns false if Var is not an environment-variable.
These env-variables should be set before you start the application.
I always use an idiom like this to start (on a bash-shell):
export PORT=8080 && erl -noshell -s program main
If you want "named" argument, with possible default values, you can use this command line (from a toy appli I made):
erl -pa "./ebin" -s lavie -noshell -detach -width 100 -height 80 -zoom 6
lavie:start does nothing more than starting an erlang application:
-module (lavie).
-export ([start/0]).
start() -> application:start(lavie).
which in turn start the application where I defined default value for parameters, here is the app.src (rebar build):
{application, lavie,
[
{description, "Le jeu de la vie selon Conway"},
{vsn, "1.3.0"},
{registered, [lavie_sup,lavie_wx,lavie_fsm,lavie_server,rule_wx]},
{applications, [
kernel,
stdlib
]},
{mod, { lavie_app, [200,50,2]}}, %% with default parameters
{env, []}
]}.
then, in the application code, you can use init:get_argument/1 to get the value associated to each option if it was defined in the command line.
-module(lavie_app).
-behaviour(application).
%% Application callbacks
-export([start/2, stop/1]).
%% ===================================================================
%% Application callbacks
%% ===================================================================
start(_StartType, [W1,H1,Z1]) ->
W = get(width,W1),
H = get(height,H1),
Z = get(zoom,Z1),
lavie_sup:start_link([W,H,Z]).
stop(_State) ->
% init:stop().
ok.
get(Name,Def) ->
case init:get_argument(Name) of
{ok,[[L]]} -> list_to_integer(L);
_ -> Def
end.
Definitively more complex than #Hynek proposal, but it gives you more flexibility, and I find the command line less opaque.
Trying to run tutorial samples for RabbitMQ in Erlang under OSX but it fails with the following message:
./send.erl:20: can't find include lib "rabbit_common/include/rabbit.hrl"
./send.erl:21: can't find include lib "rabbit_common/include/rabbit_framing.hrl"
escript: There were compilation errors.
amqp_example.erl:
-module(amqp_example).
-include("amqp_client.hrl").
-compile([export_all]).
test() ->
%% Start a network connection
{ok, Connection} = amqp_connection:start(#amqp_params_network{}),
%% Open a channel on the connection
{ok, Channel} = amqp_connection:open_channel(Connection),
%% Declare a queue
#'queue.declare_ok'{queue = Q}
= amqp_channel:call(Channel, #'queue.declare'{}),
%% Publish a message
Payload = <<"foobar">>,
Publish = #'basic.publish'{exchange = <<>>, routing_key = Q},
amqp_channel:cast(Channel, Publish, #amqp_msg{payload = Payload}),
%% Get the message back from the queue
Get = #'basic.get'{queue = Q},
{#'basic.get_ok'{delivery_tag = Tag}, Content}
= amqp_channel:call(Channel, Get),
%% Do something with the message payload
%% (some work here)
%% Ack the message
amqp_channel:cast(Channel, #'basic.ack'{delivery_tag = Tag}),
%% Close the channel
amqp_channel:close(Channel),
%% Close the connection
amqp_connection:close(Connection),
ok.
Please help me to fix this problem. Thank!!!
Erlang has macro include_lib, which can search for libraries in path and is convenient, because you don't have to specify version of library - it automatically uses newest version. So, instead of
-include("rabbit_common-3.3.5/include/rabbit.hrl").
you can just write:
-include_lib("rabbit_common/include/rabbit.hrl").
So, in your case, you have to make sure, that file rabbit_common-[version]/include/rabbit.hrl is in ERL_LIBS path. In the tutorial, you are using, they want you to download those files from here and unpack them like this:
unzip -d deps deps/amqp_client.ez
unzip -d deps deps/rabbit_common.ez
Those unpacking commands do not work on OS X, because unzip works only with .zip files. So this might be your problem. Try using another application to unpack them and double check, that the file is there. Don't forget to add ERL_LIBS=deps before compiling and running the example:
ERL_LIBS=deps erlc -o ebin amqp_example.erl
ERL_LIBS=deps erl -pa ebin
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I prepare to develop one heartbeat program, which need to send udp packet every 5s.
How to sleep 5s in erlang or is there sleep(5) function to be used?
How to make it run in background?
If you want your application to send a udp packet I would recommend you to start with a gen_server(coz. you will obviously be having need to add other functionalities to your application).
1. For sending packets at regular interval.
timer:send_interval(5000,interval),
This will call "handle_call(interval,State)" callback of gen_server every 5 seconds from where you can send your packets
2. Making it run in background.
As already posted use "run_erl". I have used this myself to run my application successfully as a daemon.
run_erl -daemon /tmp "erl"
This will create two pipes "erlang.pipe.1.r" and "erlang.pipe.1.w" under "/tmp" dir of unix and you can write commands to write pipe for starting your application using perl or any scripting lang or even c/c++ :)
Recently I have been learning the erlang programming language. One task I gave myself was to write a linux daemon.
As you probably already know, daemons are used to run unix services. Services commonly controlled by daemons include database servers, web servers, web proxies etc. In this example the server is very simple, the client calls the function "say_hi" and the server responds with "hello".
In the linux environment daemons are controlled by scripts that are stored in places such as /etc/init.d. These scripts respond according to convention to the commands start, stop and restart.
Let us start with the shell script:
#!/bin/sh
EBIN=$HOME/Documents/Erlang/Daemon
ERL=/usr/bin/erl
case $1 in
start|stop|restart)
$ERL -detached -sname mynode \
-run daemon shell_do $1 >> daemon2.log
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit 0
This has to be one of the simplest shell scripts that you have ever seen. Daemon respond to three different commands, stop, start and restart. In this script the command is simply passed through to the daemon. One improvement would be to exit with the return code from the daemon execution.
So how about the daemon? Here it is...
%% PURPOSE
%% Author: Tony Wallace
%%
%% Manage an erlang daemon process as controlled by a shell scripts
%% Allow standard daemon control verbs
%% Start - Starts a daemon in detached mode and exits
%% Stop - Attaches to the daemon, monitors it, sends an EXIT message and waits for it to die
%% Restart - Calls stop and then start
%% Log events
%% Return UNIX compatible codes for functions called from shell scripts
%% Exit shell script calls so as to not stop the scripts from completing
%% Shell scripts expected to use shell_do to execute functions
%%
%% Allow interaction with daemon from other erlang nodes.
%% Erlang processes are expected to call functions directly rather than through shell_do
%%
%% MOTIVATION
%% Erlang is great, but as an application it needs to be managed by system scripts.
%% This is particularly for process that are expected to be running without user initiation.
%%
%% INVOCATION
%% See daemon.sh for details of calling this module from a shell script.
%%
%% TO DO
%% Define and use error handler for spawn call.
-module(daemon).
%-compile([{debug_info}]).
-export [start/0,start/1,stop_daemon/0,say_hi/0,kill/0,shell_do/1].
%%-define (DAEMON_NAME,daemon#blessing).
-define (DAEMON_NAME,list_to_atom("daemon#"++net_adm:localhost())).
-define (UNIX_OKAY_RESULT,0).
-define (TIMEOUT_STARTING_VM,1).
-define (VM_STARTED_WITHOUT_NAME,2).
-define (INVALID_VERB,3).
-define (COULD_NOT_CONNECT,4).
-define (TIMEOUT_WAITING_QUIT,5).
-define (TIMEOUT_STOPPING_VM,6).
wait_vm_start(_,0) -> ?TIMEOUT_STARTING_VM;
wait_vm_start(D,N) ->
net_kernel:connect(D),
Dl = lists:filter(fun(X) -> X==D end,nodes()),
if Dl =:= [] ->
receive after 1000 -> true end,
wait_vm_start(D,N-1);
Dl /= [] -> ?UNIX_OKAY_RESULT
end.
wait_vm_stop(_,0) -> ?TIMEOUT_STOPPING_VM;
wait_vm_stop(D,N) ->
net_kernel:connect(D),
Dl = lists:filter(fun(X) -> X==D end,nodes()),
if Dl /= [] ->
receive after 1000 -> true end,
wait_vm_start(D,N-1);
Dl == [] -> ?UNIX_OKAY_RESULT
end.
flush() ->
receive
_ ->
flush()
after
0 ->
true
end.
sd(Hdl) ->
MyNode=node(),
if
MyNode =:= nonode#nohost ->
info(stdout,"~s","Error: Erlang not started with a name. Use -sname <name>"),
?VM_STARTED_WITHOUT_NAME;
MyNode /= nonode#nohost ->
Atm_daemon = ?DAEMON_NAME,
Connected = net_kernel:connect(Atm_daemon),
case Connected of
true ->
info(Hdl,"~s",["daemon process already started"]),
?UNIX_OKAY_RESULT;
false ->
info(Hdl,"~s",["starting daemon process"]),
StartString = "erl -detached -sname daemon",
os:cmd(StartString),
Vm_daemon = wait_vm_start(Atm_daemon,10),
case Vm_daemon of
?UNIX_OKAY_RESULT ->
info(Hdl,"~s",["spawning main daemon process"]),
spawn(Atm_daemon,?MODULE,start,[]), ?UNIX_OKAY_RESULT;
A -> A
end
end % case Connected %
end.
say_hi() ->
Daemon = ?DAEMON_NAME,
Connected = net_kernel:connect(Daemon),
if Connected ->
{listener,Daemon} ! {hello,self()},
receive
Response -> Response
after 10000 -> timeout end;
not Connected -> could_not_connect
end.
stop_daemon() ->
Daemon = ?DAEMON_NAME,
Connected = net_kernel:connect(Daemon),
if Connected ->
flush(),
{listener,Daemon} ! {quit,self()},
receive
bye -> wait_vm_stop(Daemon,10)
after 10000 -> ?TIMEOUT_WAITING_QUIT
end;
not Connected -> ?COULD_NOT_CONNECT
end.
shell_do(Verb) ->
{A,Hdl} = file:open('daemon_client.log',[append]),
case A of
ok ->
info(Hdl,"~s",[Verb]);
error -> error
end,
Result = handle_verb(Hdl,Verb),
info(Hdl,"Return status ~.10B",[Result]),
init:stop(Result).
%%handle_verb(_,_) -> 0;
handle_verb(Hdl,["start"]) -> sd(Hdl);
handle_verb(_,["stop"]) -> stop_daemon();
handle_verb(Hdl,["restart"]) ->
stop_daemon(),
sd(Hdl);
handle_verb(Hdl,X) ->
info(Hdl,"handle_verb failed to match ~p",[X]),
?INVALID_VERB.
kill() ->
rpc:call(?DAEMON_NAME, init, stop, []).
start(Source) ->
Source ! starting,
start().
start() ->
register(listener,self()),
case {_,Hdl}=file:open("daemon_server.log",[append]) of
{ok,Hdl} -> server(Hdl);
{error,Hdl} -> {error,Hdl}
end.
info(Hdl,Fmt,D)->
io:fwrite(Hdl,"~w"++Fmt++"~n",[erlang:localtime()] ++ D).
server(Hdl) ->
info(Hdl,"~s",["waiting"]),
receive
{hello,Sender} ->
info(Hdl,"~s~w",["hello received from",Sender]),
Sender ! hello,
server(Hdl);
{getpid,Sender} ->
info(Hdl,"~s~w",["pid request from ",Sender]),
Sender ! self(),
server(Hdl);
{quit,Sender} ->
info(Hdl,"~s~w",["quit recevied from ",Sender]),
Sender ! bye,
init:stop();
_ ->
info(Hdl,"~s",["Unknown message received"])
after
50000 ->
server(Hdl)
end.
For the reader not used to reading erlang, there some of this code is run as a result of the shell script we saw above. Other code in this file is the daemon itself. Referring back to the shell script we see that the script calls procedure shell_do. Shell_do writes log entries, calls handle_verb and exits. Handle_verb implements the different behaviours for each verb. Starting the daemon is handled by function sd, which creates the daemon by an operating system call os:cmd, waits for the erlang virtual machine to initialise, and then spawns the server code called start, which in turn calls server.
Sleep is available in erlang, through the timer functions.
http://www.erlang.org/doc/man/timer.html
For the background process, you can use the -detached cli argument.
You can specify an entry point with -s
EDIT
You can also spawn a new process from your main program:
http://www.erlang.org/doc/reference_manual/processes.html
With respect to daemonizing, consider starting your erlang program with the run_erl utility that comes with OTP. Note in particular the -daemon command line flag.