English 中文(简体)
中断埃尔兰进程
原标题:Interrupting a process in Erlang
  • 时间:2012-04-18 22:32:07
  •  标签:
  • erlang

I am new to erlang. I wonder if it is possible to interrupt a processor in erlang. Assume we have processor x executing a function f1() that takes a long time to execute. I would like to find an efficient way to interrupt the processor x to execute function f2() and after the execution of f2() it goes back to executing f1() from it was interrupted.

这样做的一种方式(尽管并非我所想的)是让1 000人(即1_proc)被一名加工者处决,而1_proc等信息的创建者,如[不间断、f1_被点击等],如果受到干扰,则处决。

然而,这不是我所希望的。 f2()取决于f1(a)? 在本案中,有 f1(a)被搁置, f2(b)被执行,然后有 f1(a)从停止。 我知道我们可以结束一个进程,但我们能够中止这一进程?

最佳回答

The answer to your question is no, this can t be done. There is no way to pause a process from the "outside" without any hook (e.g. receive clause) inside the process.

问题回答

我认为,你的问题(加工商)是令人难以置信的,因为你试图与地兰进程合作。

You should trying working with erlang hibernate command. Directly from the above doc link:

Puts the calling process into a wait state where its memory allocation 
has been reduced as much as possible, which is useful if the process 
does not expect to receive any messages in the near future.

Using timers and message passing between processes you can force your workflow. i.e. pausing one if it takes too much time, while other continues doing it work.


虽然你使用这个案例并不明确,但你可以同时(更多)同时工作,而不必相互等待,而且一旦工作完成,就会得到通知。

这样做的一个途径是简单地在不同的进程中开始两个职能。 f2(a)取决于 f1(a)的结果,receive载有所需数据的信息。 f1(a)在计算数据时,将数据发送至f2(a)程序。

If f2() reaches the receive clause too early, it will automatically pause and wait until the message arrives (hence letting f1() continue its work). If f1(), however, is done first, it will carry on with it s other tasks until preempted automatically by the Erlang process scheduler.

你们也可以通过让它等待f2(a)的电文而暂停使用。 在此情况下,确保 f1(a)等到AFTER发出信息,避免僵局。

例:

f1(F2Pid) ->
   Data = ...,
   F2Pid ! {f1data, Data},
   ... continue other tasks ....

f2() ->
   ... do some work ...,
   Data = receive
             {f1data, F1Data} -> F1Data
          end,
   ... do some work with Data ....

main() ->
   F2Pid = spawn_link(?MODULE, f2, []),
   f1(F2Pid).

这一信息传递对埃尔兰方案拟订模式至关重要。 你们不需要发明私刑或锁。 仅仅收到信息,埃尔兰将确保你获得这一信息(而且这一信息只是这一信息)。

我不知道你是如何学习埃兰兰的,但我建议由塞西里尼和安普特(O Reilly)出版的“Erlang Program。 这本书非常详细,有好的例子,你都需要了解信息传递和一致。





相关问题
How big can Erlang DETS be and what to do if its too small?

All I need is a large persistent lookup table in Erlang and dets seems like just the thing though I need a definative answer to: just how big the total size of the binaries in the table can be. how ...

passing events from erlang to Clojure

I m looking for a way to pass events back and forth between Clojure and erlang. has someone done this before? how should I encode the (immutable) messages in a flaxable general way? Should IPC be ...

How to send a push notification using Erlang?

I m trying to send a push notification to APNs using Erlang. This is the code I came up with so far: -module(apnstest2). -export([connect/0]). connect() -> application:start(ssl), ssl:...

How do I build a DNS Query record in Erlang?

I am building a native Bonjour / Zeroconf library and need to build DNS query records to broadcast off to the other machines. I have tried looking thru the Erlang source code but as I am relatively ...

AccessViolation when calling unmanaged dll

When calling an unmanaged Dll from a c# application I get an AccessViolationException. The strange thing is that the exported function has no arguments, so the problem is not in the Marshalling of ...

How to enable active sockets in a Mochiweb application?

Does anyone know how to enable active instead of passive sockets in a Mochiweb application. Specifically, I am trying to adapt http://www.metabrew.com/article/a-million-user-comet-application-with-...

How to convert numbers to words in Erlang?

I found this interesting question about converting numbers into "words": Code Golf: Number to Words I would really like to see how you would implement this efficiently in Erlang.

热门标签