简体   繁体   English

是否有任何语言为流程中的“真正”模块化提供支持?

[英]Do any languages provide support for “true” modularity within a process?

I've been reading Principles of Computer System Design (Saltzer & Kaashoek), and one of the early chapters is on modularity. 我一直在阅读Principles of Computer System Design (Saltzer和Kaashoek),而早期的章节之一就是模块化。 For example: 例如:

  • Different modules should only interact through specified interfaces 不同的模块只能通过指定的接口进行交互
  • Modules should be constructed to expose as little of their internal implementation as possible 模块的构造应尽量减少其内部实现

Now that's all pretty standard stuff, and is the way most OO languages work. 现在,所有这些都是非常标准的东西,并且是大多数OO语言的工作方式。 However, they mention even stricter requirements, such as: 但是,他们提到了更严格的要求,例如:

  • A called module should not be able to lock up the caller by failing to return 被调用的模块应该不能通过失败返回来锁定调用者
  • A called module should not be able to cause its caller to die by running out of stack space 被调用的模块不应由于耗尽堆栈空间而导致其调用方死亡

Now these requirements make perfect sense to me, and would do much to stop errors propagating through the entirety of a massive program and bring it all crashing down. 现在,这些要求对我来说已经很有意义了,并且对于阻止在整个大型程序中传播的错误并使所有错误崩溃都将起到很大的作用。

However, the method they suggest, splitting everything up into client/server processes, seems somewhat of an overkill for many purposes. 但是,他们建议的方法将所有内容拆分为客户端/服务器进程,对于许多目的来说似乎有些过头了。 Writing everything you want to modularize as client/server seems to be both: 将要模块化的所有内容编写为客户端/服务器似乎都是:

  • incredibly tedious 令人难以置信的乏味
  • slow (wrt execution speed) 慢(wrt执行速度)

For example, I would like to be able to delegate work to my Math module, and place limits on execution time and memory use, but I don't really want to have to make a separate Math server running in the background just for these benefits! 例如,我希望能够将工作委派给我的Math模块,并限制执行时间和内存使用,但是我真的不想为了这些好处而不得不在后台运行单独的Math 服务器 Furthermore, the IPC message-passing overhead (both computation and latency) is certain to be pretty huge compared to direct procedure calls. 此外,与直接过程调用相比,IPC消息传递开销(计算和等待时间)肯定会非常大。

Are there any languages that provide this kind of modularity within a process? 是否有任何语言流程中提供这种模块化? As an in between before the tight-coupling of direct procedure-calls and the overhead of a multi-process client-server design? 在直接过程调用与多进程客户端-服务器设计的开销紧密耦合之前,是介于两者之间吗? The way I envision it, instead of : 我设想的方式,而不是:

y = Math.sin(x)

which leaves me open to infinite loops and stack-overflows in the Math module, I would like to do something like 这让我在Math模块中容易陷入无限循环和栈溢出的Math ,我想做些类似的事情

y = try(maxMemory=1024kb, maxTime=12ms){
    Math.sin(x)
}catch(Anything){
    0
}

Which would set y to a default value ( 0 ) if anything fails within Math.sin . 如果Math.sin发生任何故障,它将y设置为默认值( 0 )。 Invalid input, bugs, infinite loops, runtime-exceptions, anything. 无效的输入,错误,无限循环,运行时异常等。 Does anything like this exist already in some language, and if not, why? 某种语言是否已经存在类似的东西,如果没有,为什么? It seems to me like something that would be immensely useful, in a wide variety of places. 在我看来,这种东西在很多地方都非常有用。

You can do this in Racket using the sandbox library : 您可以使用沙箱库Racket中执行以下操作:

> (require racket/sandbox)
> (define x 0) ;; some input value
> (call-with-limits 0.012 ;; 12 ms
                    1     ;; 1 MB
                    (lambda () (sin x)))
0

> (call-with-limits 1 ;; 1 sec
                    1 ;; 1 MB
                    (lambda () (sleep 2) 'done))
with-limit: out of time

> (call-with-limits #f ;; no time limit
                    1  ;; 1 MB
                    (lambda () 
                      (let loop () 
                        (cons (make-vector 10000) (loop)))))
with-limit: out of memory

The code is run in a separate lightweight Racket ("green") thread (not a separate OS process). 该代码在单独的轻量级Racket(“绿色”)线程中运行(而不是单独的OS进程)。

The last two cases raise exceptions, which you can catch if you want to substitute an alternative value. 最后两种情况引发异常,如果要替换替代值,则可以捕获该异常。

Using the sandbox library you can also do more powerful things like set up evaluators that restrict access to a small number of approved libraries, restrict filesystem and network access, etc. 使用沙盒库,您还可以执行更强大的功能,例如设置评估程序,以限制对少数已批准库的访问,限制文件系统和网络访问等。

On the other hand, this is more expensive than a normal function call. 另一方面,这比普通的函数调用昂贵。 In practice, you need to figure out which components you trust and which ones you don't. 在实践中,您需要弄清楚您信任哪些组件,而不信任哪些组件。 Or perhaps you should look at systems that support static verification of your additional requirements. 或者,也许您应该看一下支持对附加要求进行静态验证的系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM