erlang

How to give different priorities to erlang messages

杀马特。学长 韩版系。学妹 提交于 2019-12-23 15:59:02
问题 In the book "erlang programming" and in this stack overflow question I have seen that we can give different priorities to erlang messages in the mailbox using: I tried to implement something similar with this code: -module(mytest). -export([start/0, test/0]). start() -> register(?MODULE, spawn(fun() -> do_receive() end)). do_receive() -> receive {high_p, Message} -> io:format("High priority: ~p~n", [Message]), do_receive() after 0 -> receive {low_p, Message} -> io:format("Low priority: ~p~n",

What's the point of meck:validate?

亡梦爱人 提交于 2019-12-23 15:56:42
问题 As a newcomer to meck, I've been putting together a test that shows the various features. I cannot, however, understand why a developer might call meck:validate. Here's my example: -module(meck_demo). -include_lib("eunit/include/eunit.hrl"). validate_is_of_limited_use_test_() -> { foreach, fun setup_mock/0, fun cleanup_mock/1, [fun validate_does_not_fail_if_a_function_is_not_called/0, fun validate_does_not_fail_if_a_function_is_called_with_wrong_arity/0, fun validate_does_not_fail_if_an

Web server for running PHP+Erlang

末鹿安然 提交于 2019-12-23 14:58:04
问题 I wish to run PHP and Erlang on a web server. Apache is ruled out, because the backend Erlang process would need to handle around 3000 requests concurrently. So definitely something with a smaller memory footprint like lighttpd... Which one would you recommend? And why? 回答1: Erlang has built-in web server: http://yaws.hyber.org/ Which you can use. It is supposed to be really really fast. If you want to expose Erlang to the web, Yaws is the way to go (in just learning Erlang, so this may be

Convert tuples to proplists

99封情书 提交于 2019-12-23 12:59:46
问题 How can I convert tuple from MongoDB {'_id',<<"vasya">>,password,<<"12ghd">>,age,undefined} to proplist [{'_id',<<"vasya">>},{password,<<"12ghd">>},{age,undefined}] 回答1: Assuming you want to basically combine the two consecutive elements of the tuple together, this isn't too hard. You can use element\2 to pull elements from tuples. And tuple_size\1 to get the size of the tuple. Here's a couple of ways to handle this: 1> Tup = {'_id',<<"vasya">>,password,<<"12ghd">>,age,undefined}. {'_id',<<

erlang - how can I match tuple contents with qlc and mnesia?

青春壹個敷衍的年華 提交于 2019-12-23 12:19:17
问题 I have a mnesia table for this record. -record(peer, { peer_key, %% key is the tuple {FileId, PeerId} last_seen, last_event, uploaded = 0, downloaded = 0, left = 0, ip_port, key }). Peer_key is a tuple {FileId, ClientId}, now I need to extract the ip_port field from all peers that have a specific FileId. I came up with a workable solution, but I'm not sure if this is a good approach: qlc:q([IpPort || #peer{peer_key={FileId,_}, ip_port=IpPort} <- mnesia:table(peer), FileId=:=RequiredFileId])

communicating between http handler and websocket handler in Cowboy

白昼怎懂夜的黑 提交于 2019-12-23 11:57:43
问题 I'd like to create a websocket app in Cowboy that gets its data from another Cowboy handler. Let's say that I want to combine the Echo_get example from cowboy: https://github.com/ninenines/cowboy/tree/master/examples/echo_get/src with websocket example https://github.com/ninenines/cowboy/tree/master/examples/websocket/src in such a way that a GET request to Echo should send a "push" via websocket handler to the html page in that example. What is the least complicated way to do it? Can I use

dialyzer not detecting guard violation when function is exported

让人想犯罪 __ 提交于 2019-12-23 10:57:05
问题 Dialyzer version 2.9. Erts 7.3. OTP 18. In the following contrived erlang code: -module(dialBug). -export([test/0]). %-export([f1/1]). % uncomment this line test() -> f1(1). f1(X) when X > 5 -> X*2. When dialyzer is run over the above code it warns that the code won't work as the guard test (X > 5) can never succeed. However, when I uncomment the 3rd line and export the f1/1 function dialyzer no longer issues any warnings. I realise that when f1/1 is exported it is impossible for dialyzer to

Determining if an item is a string or a list in Erlang

耗尽温柔 提交于 2019-12-23 10:54:11
问题 I am writing a program that can have either a list or a string as an argument. How can I tell the difference between a string and a list programmatically in Erlang. Something like: print(List) -> list; print(String) -> string. 回答1: io_lib:printable_list might be what you are looking for. However it doesn't handle unicode only latin-1 encodings. If you need to detect unicode strings I think you might be out of luck. The best bet is pseudo typing your lists like so: {string, [$a, $b, $c]}. Kind

How do I validate an Erlang config file from a linux command

半城伤御伤魂 提交于 2019-12-23 10:49:14
问题 I can validate a config file with running the command file:consult("settings.config"). Can I do this from a linux command? I know I can open the erl shell and run this command, but I want to do this with one shell command 回答1: You could use an escript file to do this. Something like this: validate.escript #!/usr/bin/env escript main([ConfigFile]) -> {ok, Terms} = file:consult(ConfigFile), io:format("~p~n",[Terms]). Then you can invoke it from the command line: ./validate.escript path/to/file

RabbitMQ-故障记录-RabbitMQ-error:unable to connect to node 'rabbit@HAPP11': nodedown

你。 提交于 2019-12-23 10:18:42
目录 问题 解决方案 实例 验证 问题 重装,安装RabbitServer之后,运行命令提示: error:unable to connect to node 'rabbit@HAPP11': nodedown 解决方案 根据提示, TCP connection succeeded but Erlang distribution failed。 意思是,tcp连接正常,但是Erlang配置错误,由于Erlang是按照cookie识别,所以进行排查。 更正Erlang的cookie Erlang会生成两个cookie文件:C:\Windows\.erlang.cookie 及 C:\用户\计算机用户名\.erlang.cookie。检查两个文件内容是否一致,不一致用其中一个替换另一个即可。 实例 【图:两个.erlang.cookie内容不一致】 验证 修改确认一致之后, 执行rabbitmqctl status命令检查状态。 cmd>rabbitmqctl status 参考资料: RabbitMQ报错Error: unable to connect to node rabbit@xxx: nodedown的解决方式 - 欧神的博客 - CSDN博客 https://blog.csdn.net/u012930316/article/details/76841025 来源: CSDN