Download - 如何写出健壮且符合otp设计规范的erlang / elixir程序
����• erlang
����/$���2(����'�"%
• otp
�)!���* ��erlang16�0�supervisor5gen_server5ets�
• beam
-�erlang���,3�6erlang+����.4 beam��
• erts
erlang����'6�0�beam5kernel��
• elixir
�����beam,3��"%
2
�����func%on����module��
��module�������beam�
module��func*on��� ���������func*on����module���
erlang�������module����������� �1�module�
elixir�module����������� ����module�
erlang������������erlang��1�elixir���Kernel������elixir����������� Elixir.���
1" ��erlang�����������������system_info�
4
Actor��erlang�����������actor ��
actor��������actor�
��actor����� ��actor��������������
actor������������ �
5
erlang���erlang������process ��func-on��process����
��process�� ������ ���
process��������������� ���process����������������
erlang:system_info(process_limit).�������process���� �2621442�
process������� ����
�process�������mailbox ����process������ ��process��� ����������receive�
2"���� �����������
6
��������Pid$=$spawn(Mod,$Func,$Args)������������������������-export ���������
Pid$=$spawn(Fun)���������Fun��������� ���
Pid$!$Message�����Message�Pid��Pid1!Pid2!...!Msg���Msg����PidX
receive&...&end��������������� ����������receive�
7
���receive��1.��+a$er�6RN���'mer
2.!'mailbox���/<-�H>QBRK�:D$mailboxLC�M��1#?>
3.K��:D$mailboxLC )save2queueRP(&�<-3;��/<-QB�87��<-�P(QB"O
4.����<-QBR*A%�20receive�9�<-R�<-���P(�<-Rsave2queue�P(QB
5.���<-QBRsave2queue�<-%�J5�=4*A�EI*)mailboxR����@G�'merRD'<'mer
6.��'mer, RM�a$er�6.�#?>RFsave2queue�<-%�J5�=4*A�EI*)mailbox
8
�����������pid� �������pid�
�������!��"�!��'�� ��! �' ����!#�registered(process'�����%���$������!���&
���� ���������������������� ����������� �����
9
������process����associa)ve+array��������������� ���������
erlang:get().erlang:put(a, 1).erlang:get(a).erlang:put(b, 1).erlang:get(b).erlang:put(b, erlang:get(b) + 1).erlang:get().erlang:erase(b).erlang:get().erlang:erase().erlang:get().
10
���������erlang� �������������!�����������!������������
��
11
������������ ��catch�����exit(normal)��������
�������������catch� ���exit(Reason)�Reason�normal� �atom���������
12
������������������������������ ��������
�������signal����������signal��� �������
��� �����normal'exit�� ����signal�
���� ������abnormal(exit�����������signal�������������
� ������ �������13
��������������� ������process_flag(trap_exit, true).������ ���
��������signal�signal����{'EXIT', Pid, Reason}� �������mailbox�
14
�������������������������������� ��������� ������
��� ����� ����{'DOWN',Ref,process,Pid,Why}��� ������������
15
�����������exit(Pid, Reason)�Pid����� ����Reason�exit,signal�Pid��
����normal��kill�� �������������{'EXIT', From, Reason}���From���������pid�
����normal������ ���������{'EXIT', From, normal}���
���kill������exit'signal����Pid����������� ��������killed�
16
������catch Expression���� ��exit(Reason)������
catch����exit(Pid, Reason)�
catch�����
throw������#����catch��� �# �������#���catch��!�����"
17
erlang������• ����1%-9!
��link'monitor�0�1A) �2��1'&�;"��17�A) ����1;"��1�.��A#�$��?8�(3�1A?�4@
• ��1=<
��������1=<A� *) ';"��1�+>/�:5>/#�1���6,@
18
gen_server
����Client'Server���Server��� ��
��� ��callback"�������Server��%�������#��� !�%�������$
gen_server module Callback module----------------- ---------------gen_server:start_link -----> Module:init/1gen_server:call -----> Module:handle_call/3gen_server:cast -----> Module:handle_cast/2other message -----> Module:handle_info/2exit -----> Module:terminate/2Dynamic Code Upgrade -----> Module:code_change/3
19
supervisor�� �����������������
�������supervisor���worker��
worker��gen_server�gen_fsm�gen_event������� ����
supervisor�init�������������� ��
ChildSpecs = [child_spec()]child_spec() = {Id,StartFunc,Restart,Shutdown,Type,Modules} Id = term() StartFunc = {M,F,A} M = F = atom() A = [term()] Restart = permanent | transient | temporary Shutdown = brutal_kill | int()>0 | infinity Type = worker | supervisor Modules = [Module] | dynamic Module = atom()
20
����������• one_for_one&�$#����
• one_for_all&��1���#�&�%�������"�$ �!���$
• rest_for_one&$ �!�#����������%��"��!���$
• simple_one_for_one&� one_for_one&������ ������
21
applica&on
��������� �����������
start����������supervisor
�erlang�appname.app.src�elixir�mix.exs������������' ���$���������!�� '"����start%����#��$��&
22
��otp��� �����1. ���@R'O��nq�\4��applica(on
2. �\4�8P^H
3. �7I?'Oaq,3�"�supervisorq"�server
4. ��'O+,38P=#:V
5. 'O���C-�j_$@q&Uk&
6. �7.E!'O�5D*q'O�\�P���q�\4�<f
7. 'Ob1c���\4�oLq�7�oL
8. BK>A�d�TYq=�G>A�=�'Od�
9. WV0N%SBK'O�9��`q�\pF��� ��ml
10. 'O�g��$hX[
11. �ZQ(�I?qi6L])q;eJ��M2�-/
23
��������1.2�catchEXV,�Tc �gen_server:call$
2.2���#B�7+AbMcZ:.�#B��>Q
3.=L�6D#B^K#B��"��8(#B4K�GJW�
4.\S538(�F]�#BcF]#BW�� ?��
5.`a�9�GJ#BW���TcZ:![#BW�GJ#B'R;P-�&_>
6.2�ETScZ:R;P-
7.NE�F]�I1���U�6D#B %�%)*A��V,��
8.O0#BY��/��@C*AR;cI�<H�W�*A
24
��observer
�shell���observer:start().��observer�
������applica&on��!��applica&on������!�������!ETS�����!����� �!����� �!������
25
erlang��������• -^�}B�@���-^• rQp&�
erlang ��ETS�DETS_�b"3_�large,binaries_t%off2heap,storage,area��!��-^�ETS,b"�Jh�CAyz4cfL)��U1��@�public,�
• rQeiOZ|
+�I/O���?R m�q�distributed,algorithm�+�distributed,hash,tables�• rQ`\���-^
�5)3_�S�XI`\-^�@�GT9$���<�`\1-^�6;�MCE5)3_��=v�/-^�rQ@�GT9$�k]o#Pn��<Vs'�Da�CPU,CoreA�
• N>wK2�*CEWl�^i
@�mapreduce�F.uN^i��%cpu2bound�^i��P�*�rQ��K2>w�(��erlang K2>w{[3_�d���CE~g�%Y:���H�q�8F.��>wK2���74K2'o#~g�Wl0�$<��jx�
26