带有嵌入式 IronPython 的 RESTful Web 服务:引擎和引擎范围问题
我有一个 RESTful C# Web 服务(使用 Open Rasta),我想运行与 CouchDB 通信的 IronPython 脚本。
我需要澄清的一件事是:我多久需要一次 python 引擎和范围的新实例?每个应用程序各一个?每个会话?根据要求?
我目前有一个应用程序级别的静态引擎以及已编译脚本的字典;然后,根据请求,我创建一个新范围并在该范围内执行代码...
这是正确的吗?线程安全?并尽可能地表现?
编辑:关于赏金请同时回答我在回复 Jeff 时提出的问题:引擎的静态实例是否会导致来自不同客户端的顺序请求排队等待执行?如果是这样,我可能会根据每个请求需要一切。
I have a RESTful C# web service (using Open Rasta) that I want to run IronPython scripts that talk to a CouchDB.
One thing I could use some clarification on is: How often do I need a new instance of the python engine and the scope? one each per application? per session? per request?
I currently have a static engine at the application level along with a dictionary of compiled scripts; then, per request, I create a new scope and execute the code within that scope...
Is that correct? thread safe? and as performant as it could be?
EDIT: regarding the bounty Please also answer the question I posed in reply to Jeff: Will a static instance of the engine cause sequential requests from different clients to wait in line to execute? if so I will probably need everything on a per-request basis.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每个应用程序一个 ScriptRuntime/ScriptEngine 和每个请求一个范围正是应该如何完成的。运行时/引擎是线程安全的,而作用域不是。
A ScriptRuntime/ScriptEngine per application and a Scope per request is exactly how it should be done. Runtimes/Engine are thread-safe and Scopes are not.
除非所有代码都是线程安全的,否则按请求是可行的方法。使用每个应用程序可能会获得更好的性能(每个会话意味着您在客户端和服务器之间有“会话”的概念),但是这意味着“应用程序”中的所有代码都是线程安全的。
因此,除非您知道代码是线程安全的,否则您应该使用每个请求。
另请注意,只有在以下情况下,每个应用程序才会更快:
你没有阻塞任何线程
方式。
业务层/数据层分别是
非常“重”(需要很多
实例化的时间)然后一些
可以获得性能优势。
Per request is the way to go unless all of your code is thread safe. You may get better performance using per application (per session implies you have th notion of "sesions" between you client and server), however the implication there is that all of your code in the "application" is thread safe.
So per-request is what you should use unless you know your code to be thread safe.
Note also that per application will be faster only if:
you've not blocking threads in any
way.
business layer/data layer are
extremely "heavy" (take a lot of
time to instantiate) then some
performance benefit may be gained.