gpt4 book ai didi

erlang - 为什么erlang io是:format output being lost and what do I need to do to restore it?

转载 作者:行者123 更新时间:2023-12-02 00:04:06 26 4
gpt4 key购买 nike

我正在编写一个 gen_server,我们将其称为 gen_server_db,没有什么特别之处。它使用的库 (emysql) 在尝试查找数据库服务器(在 gen_server_db:init() 中)时可能会遇到连接失败。当我捕获异常并尝试将某些内容打印到控制台时,我得到了 zip.下面的代码示例中的 io:format 消息都无法到达控制台。我似乎记得很久以前听过一个原因,但我不记得为什么。

init(Args) ->
% I've condensed the way I actually get this stuff to one line, but if I have a
% database online I connect properly, so I know that I'm getting the Host, User, etc.
{Host, User, Password, DB, PoolSize} = application:get_env(gen_server_db, config),

init_mysql_connection(gen_server_db_pool, PoolSize, User, Password, Host, DB),

% When the net connection to the db is down, I never get here.
ok.

init_mysql_connection(bgo_db_pool, PoolSize, User, Password, Host, DB) ->
try
emysql:add_pool(bgo_db_pool, PoolSize, User, Password, Host, 3306, DB, utf8)
catch
exit:failed_to_connect_to_database ->
io:format("Cannot connect to the mysql database server. Retrying in 1 sec.~n"),
timer:sleep(1000),
init_mysql_connection(bgo_db_pool, PoolSize, User, Password, Host, DB);
Error:Reason ->
io:format("Database connection error: ~p:~p~n", [Error, Reason]),
1/0
end.

最佳答案

所有通过io:format发送的I/O都会发送到当前group_leader正在运行的进程。然后,该进程负责使用 io:fwrite 打印消息。

如果您只想在编码时转储一些调试信息,可以使用 erlang:display/1

关于erlang - 为什么erlang io是:format output being lost and what do I need to do to restore it?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10342085/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com