Http://wenku.baidu.com/link?url=AUQR8Hn-e-fEB_lqjXsd8XfapWj1qAK7J05JoBXFib_ Llsk5qsotia8hixnv1xkezi-khfsh18qb9ned5pkipb8h6odfvr6kg75musyaamm
Erlang Learning Notes
I. The focus of Erlang language features
1. Catch is a tuple that returns the value of an expression or an error message
Try...catch is able to capture different types of errors and have process control
2, the message will never fail, if you try to send a message to a non-existent process, will only be discarded information, but will not produce a run error
3. Sending a message to a nonexistent "registration process" causes a badarg error, thus terminating the calling process. If you do not want to cause the calling process to terminate, you need to use Try...catch to protect the call.
4. Flush the Mailbox Statement flush ()
5, process links are bidirectional, so process A and B are connected or process B and a connection is irrelevant, the results are the same. If a link process terminates abnormally, the exit signal is sent to all processes that are connected to the terminating process. The exit signal is a tuple with {' Exit ', Pid,reason} format, and ' exit ' is a primitive.
6. Capture Exit Signal Process_flag (trap_exit,true)
7, the link is bidirectional, in order to monitor the process, can call Erlang:monitor (Process,proc), Proc can be a process identifier can also be registered name. When a process with a process identifier terminates, the message {' Down ', Reference,process,pid,reason} is sent to the monitoring process. Repeated calls to Erlang:monitor (PROCESS,PID) return different references to establish multiple independent monitoring. For insurance purposes, use Erlang:demonitor (Reference,[flush]), which will remove all ' down ' information provided by Reference while monitoring is turned off.
8, the macro parameters as a string to keep, you can prefix the variable before?? (such as?? Call) to reach the destination-define (VALUE (call)), Io:format ("~p = ~p~n", [?? Call,call]). Test1 ()? VALUE (Length ([+)]).
See 170
9. Pre-defined macros? MODULE? Module_string? FIKE? Line? Machine See 170
10. Use defined functions as parameters of other functions fun m:f/n hof1:filter (fun hof1:palin/1,[[2,2],[2,3]).
11. List resolved Protection element returns TRUE or False
Wk_ad_begin ({pid:21}); Wk_ad_after (, function () {$ ('. Ad-hidden '). Hide ();}, function () {$ ('. Ad-hidden '). Show ();});
12. Use ets:fun2ms to return a matching protocol
MS = ets:fun2ms ({name,country,job}) when job/= cook-and [country,name] end). Note that fun must be a literal function, which is the function entered in the ETS:FUN2MS/1 call, not the one that binds a variable. If used in a module, you need to include a header file:-include_lib ("Stdlib/include/ms_transform.hrl").
13. The Default keyword location in the ETS table is the first element of a tuple. In the record, this position is reserved for the type of record, unless the location of the keyword is explicitly specified, otherwise the desired behavior cannot be obtained. By using the expression #recordtype.keyfield to get the position of Keyfield in RecordType, you can add {keypos, #RecordType. Keyfield} to the list of options in the ETS:NEW/2 function call.
14. ETS:LOOKUP/2 returns a complete list of records that match the matching criteria, ETS:MATCH/2 returns a value that matches the variable, ETS:MATCH_OBJECT/2 returns a tuple of all matching patterns. See 224-225
15, when traversing the table need to use SAFE_FIXTABLE/2 lock table, because in the time of the destructive operation can cause a run-time error or worse, will lead to unpredictable behavior, it is a good idea to encapsulate the traversal operation in the Catch statement, Because we need to ensure that the table is released when a run-time error occurs.
16, if you want to send information to the node [email protected] a process called frequency, need to use {Frequency,[email protected]}! Message
17, if a node can communicate with other nodes, it is called the inventory node. Using Erlang:is_alive () to test whether the local runtime system is alive, using the Net_kernel module function can change the system's survival status, you can use the node/0 built-in function to find the name of the current node ([email protected])
18, the long name node can only communicate with other nodes with long names, the short name node can only communicate with other nodes with short names.
19. In the basic Erlang RPC implementation, the function call is replaced by the sending and receiving of a message p255
20. The function of the Gen_server module start will generate a new process, and the process uses the supplied parameters to invoke the init (Arguments) callback function in the Callbackmodule module. The function init must initialize the server's loopdata and return a tuple in the format {ok,loopdata}.
21,-behavior (gen_server) instruction, just tell the compiler, you use the module is Gen_server callback module, because
This he expects many callback functions.
22. It is customary to put records and macro definitions into an include file so that they can be shared across multiple modules throughout the project.
23. How to use Erlang random functions:
First set up seed
{A1,A2,A3} = Now (),
Random:seed (A1, A2, A3), and then call Random:uniform ()
24, monitoring process specification Restartstrategy (restart policy) contains 4 policy options: One_for_one, One_for_all, Rest_for_one,simple_one_for_one
Where the restart policy is Simple_one_for_one, the callback function INIT/1 does not create the child process.
Second, Erlang start parameter miscellaneous brocade
1. View the maximum number of ports in Erlang:
Enter the following instruction in the Erlang terminal and view the Max_fds value 1> erlang:system_info (check_io).
[{Name,erts_poll},
{primary, ' waitformultipleobjects '}, {Fallback,false}, {Kernel_poll,false}, {memory_size,7001}, {total_poll_set_ size,3}, {Lazy_updates,false}, {Batch_updates,false},
{Concurrent_updates,false}, {max_fds,2048}]
The maximum number of ports is modified by modifying the operating system's environment variable (erl_max_ports). For example, Linux is added in the shell: Export erl_max_ports=1024000
2, by reference to ensure that the request and response package is the same session
Make_ref (), which is almost unique within the life cycle of a node, is repeated for 2 of the 28-second calls, as detailed in the Erlang Programming Guide, page 211
Third, online collection
1, connected to the database how to query this error: Mysql:fetch (p1,<< "select * from SERVICE" >>). * * Exception error:no function clause matching
Mysql:fetch (p1,<< "select * from SERVICE" >>)
Note that the official Googlecode test code is old and the fetch interface actually needs a binary list, so it is [<< "select * from table" >>] format instead of << " SELECT * FROM table >> format.
2, there is a gen_server process, can I register the local and global two names? Because my app crosses nodes, I want local access to use locally, other nodes use global, don't know if it works?
In the Gen_server start function start and Start_link, you can specify a name of local or global for the process. But according to your requirements, in fact, in INIT/1, you can register a process by ERLANG:REGISTER/2 or GLOBAL:REGISTER_NAME/2 for another name. Because local is a data that is maintained internally by the Erlang virtual machine, and global uses ETS to maintain the data, a process can have both local and global name.
For more information, please refer to. Code for Stdlib/src/global.erl and Stdlib/src/gen.erl.
3. Several methods of accessing Erlang control console
When debugging in window we can run the Erlang node by starting multiple cmd windows, in production we need the Erlang service to run in the background on the CentOS server, which requires the detached to be added at boot time to disengage from the terminal:- Detached starts the Erlang runtime system detached from the system console. Useful for running daemons and backgrounds processes. Implies-noinput.
For our own services, even if the deployment to the production environment must be "like a magician's knife, shot but did not sell", or need some way into the Erlang background process to do some work such as: To view the runtime information of an Erlang node (memory, number of processes, etc.), Let the service gracefully exit rather than kill the process, or do a hot update (see: [Erlang 0010] Erlang Hot update of course the hot update can be simplified using the Reloader.erl scheme); At first the server was relatively small, we used the JCL way to deal with;
Erlang Shell JCL
JCL is a running mode of the Erlang shell, which is the job Control mode (JCL, in which jobs can started, killed, detached and connected). We started two nodes to complete this Operation;
Node_1 added the-detached option to run directly in the background after startup and did not start the shell Erl-setcookie abc-name [email protected]-detached node_2 used the same COO as Node_1 Kie, after booting into Erlang shell interface Erl-setcookie abc-name [email protected] below we start in [email protected] Walkthrough Jcl:eshell V5.9 (Abort wit H ^g)
([email protected]) 1> node (). % current This is in node_2 ' [email protected] '
([email protected]) 2>%control + G Enter JCL mode User switch command--H
c [NN]-connect to Job I [NN]-interrupt job K [nn]-kill job J -List all jobs s [Shell]-Start local shell R [node [Shell]]-Start remote shell q-quit Erlang
? | H-this message
--R ' [email protected] '% try to connect to [email protected]--J
1 {shell,start,[init]}% List all jobs * * [email protected] ', shell,start,[]}
--C 2 Here 2 is the job number, switch to Job 2
Eshell V5.9 (abort with ^g)
([email protected]) 1> node (). % Note prompt, now already in node_1 ' [email protected] '
([email protected]) 2> Erlang:now (). {1326,801888,347570}
([email protected]) 3>% again control + G User switch command
--J 1 {Shell,start,[init]}
protected {' [email] ', shell,start,[]}
--C 1% switch to Job 1 ([email protected]) 2> node (). % Note prompt, we have returned to node_2 ' [email protected] ' ([email protected]) 3> copy Code
Erlang Learning Notes