我正在构建一个客户/银行erlang项目,我希望将元组列表传递给各个线程(ProcessID),以便每当线程更改列表中的某些值时,其他线程/进程也可以在相同的副本上工作名单。
这是必需的,因为我希望客户线程进行一些计算并从银行中扣除余额。 到现在为止,我已经能够为所有银行和客户创建一个流程,并且客户能够调用银行流程并扣除资金,但是这种扣除仅发生在客户流程所拥有的银行清单副本中。 我该如何获取元组列表的通用/全局副本或“按引用传递”副本,以便可以通过不同的流程相互协作。
$curl_opts[ CURLOPT_POSTFIELDS ] = ['file' => new CURLFile($_FILES['tmp_name'], null, $_FILES['name']) ];
我上面使用的BankPidList看起来像这样:
Pid = spawn(customer, bazCust, [Name, Loan]),
T = {Name, Pid, Loan},
Pid ! {self(), Name, Loan, Pid, BankPidList},
get_feedback(),
我对Erlang刚起步(仅4天),所以可能有些艺术我使用的效率不是很高,但是我现在只需要完成一些与效率无关的功能即可。
答案 0 :(得分:2)
对于所有最近在银行/客户中充满疑问的银行/客户:
在erlang中,术语<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="movie"
type="com.example.tmdbclient.model.Movie" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="@+id/cvMovie"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:elevation="3dp"
app:cardCornerRadius="3dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/ivMovie"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:scaleType="fitXY"
bind:posterPath="@{movie.posterPath}" />
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/ivMovie"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:text="@{movie.title}"
android:textColor="@color/colorPrimary"
android:textSize="15sp" />
<TextView
android:id="@+id/tvRating"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tvTitle"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:text="@{Double.toString(movie.voteAverage)}"
android:textColor="@color/colorPrimary"
android:textSize="15sp" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</layout>
不存在。它们称为thread
。基本的实现是无关紧要的。在您有关erlang的任何帖子中,请不要再说processes
。
您的老师很糟糕。如果您真的想学习erlang,请购买thread
,阅读并进行所有练习。
我正在构建一个客户/银行erlang项目,我希望通过一个列表 元组到各种线程(ProcessID)的位置,以便每当一个线程 更改列表中的某些值,其他线程/进程也可以使用 列表的相同副本。
您涵盖了OTP gen_servers吗? gen_server建立客户端与服务器的关系,客户端可以向gen_server询问列表的当前值。客户还可以更改列表的值。 gen_server进入一个递归循环,如下所示:
Programming Erlang
启动gen_server时,状态可以是列表,例如loop(State) ->
receive
{get_state} ->
%send State to client
loop(State);
{set_state, 10} ->
loop([10|NewState])
end
end
或[]
。客户端可以将消息发送到服务器,以获取State变量的值或设置State变量。
我刚接触Erlang(仅4天)
然后您将无法编写该程序:您所要做的就是请其他人为您编写代码。那有什么好处?为什么您的老师认为在学习了erlang 4天后您可以编写一个多进程程序?
现在,我能够为所有银行和客户创建流程,并且 客户可以致电银行流程并扣除款项,但是 这种扣除只发生在银行清单的副本中, 客户流程了。如何获得通用/全局副本或 元组列表的按引用传递副本,以便可以相互引用 通过不同的流程进行工作。
erlang(和所有其他功能语言)的主要原理是,没有进程可以更改另一个进程正在处理的数据。您所要做的就是将数据副本发送到另一个进程。如果银行流程需要知道客户帐户中有多少,则可以在递归循环中跟踪余额:
[1, 2, 3]
您还可以在接收子句中添加init(Deposit) ->
loop(Deposit).
loop(Balance) ->
receive
{From, {deduct, Amount}} ->
case Balance >= Amount of
true ->
From ! whatever,
bank(Balance - Amount);
_ ->
From ! whatever
bank(Balance)
end
end.
消息:
deposit
{From, {deposit, Amount}} ->
From ! whatever,
bank(Balance + Amount)
也可以是一个元组列表,其中每个元组都是一个客户帐户,具有更合适的变量名,例如Balance
。要在Balances
列表中查找客户,可以使用Balances
;要删除旧余额,可以使用lists:keyfind()
,然后将新余额添加到列表中,可以执行以下操作:lists:keydelete()
。
答案 1 :(得分:0)
语言并不太复杂,我通过构建银行模块并将其过程ID传递给所有其他客户来完成我的项目。客户对此流程编号进行更改(通过
发送消息/邮件PID!{IDENTIFIER,{parameters}}
而不是对其自己的本地副本进行更改。 通过使用模块名称(或某些特定名称)注册processId,可以使您的工作更加轻松
register(Name, Pid)
但是对于像我这样的初学者来说,当您重新运行模块时,它会给出很多错误(不知道为什么,但是解决方案是终止进程并再次编译)。同样,我是Erlang的初学者,可能会错过正确的语言特定术语。 以下是我的代码的链接 https://github.com/NitinNanda/Scalable-Message-Passing-Bank-Client.git