为什么不能在IronPython中更改缓存模块中的变量? [英] Why can't I change variables from cached modules in IronPython?

查看:49
本文介绍了为什么不能在IronPython中更改缓存模块中的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:我是python和IronPython的新手,所以很抱歉.

Disclaimer: I am new to python and IronPython so sorry if this is obvious.

我们有一个使用IronPython执行脚本的C#应用​​程序.有一些通用的模块/脚本,然后有许多小的脚本,它们定义参数,进行设置,然后调用核心模块中的函数.在最近增加了一些通用模块之后,更大的性能影响了进口.我尝试通过确保只创建一个引擎并为每个要运行的脚本创建作用域来解决此问题.我已经看到了这些信息是在引擎上编译的,但是显然这样做并非如此,因为它继续花费过多时间导入它们,因此必须将其缓存在范围中.然后,我使用 THIS 博客条目创建了一个自定义共享字典,其中我可以在应用程序加载时预编译通用模块,然后再使用它.一切工作正常,直到我意识到变量在后续运行中没有改变.创建运行脚本的作用域之后,我将添加一个必需的变量...

We have a C# application that uses IronPython to execute scripts. There are a few common modules/scripts and then a lot of little scripts that define parameters, do setup, then call functions in the core modules. After some recent additions made the common modules larger performance took a hit on the imports. I attempted to fix this by making sure we only created one engine and created scopes for each script to run in. I've seen information that these are compiled on the engine, but this is apparently not so as it continued to take excessive time importing them so it must be cached in the scope. Then I used THIS blog entry to create a custom shared dictionary where I could precompile the common modules at app load and then reuse it. Everything was working fine until I realized that variables were not changing on subsequent runs. After creating a scope in which to run a script I would add a required variable...

currentScope.SetVariable("agr", aggregator)

首次运行agr的脚本在脚本中工作正常,例如,实例 A .在随后的运行中,将创建一个新的作用域,并创建一个新的 aggregator (将其称为 B )并将其设置为agr,但是当基础模块调用agr时,它不是聚合器 B ,其聚合器 A ,我已不再有效.我什至试图强制将其添加到主脚本中...

The first time this runs agr works fine in the scripts and is say instance A. On subsequent runs a new scope is created, a new aggregator is created (let's call it B) and set as agr, but when the underlying modules call agr it is not aggregator B, its aggregator A which i no longer valid. I have even tried to force it adding this to the main script...

CommonModule.agr = agr

#Do Work

CommonModule.agr = None

无济于事. agr本身不存储在共享符号字典中,但是CommonModule可以存储,并且具有agr变量.我该怎么做才能更改此变量,为什么以这种方式将其缓存?

to no avail. agr itself is not stored in the shared symbol dictionary, but CommonModule is and it has a variable for agr. What do I have to do to change this variable and why is it cached in this manner?

更新说明:很抱歉造成混淆,但这是C#和python中太多代码的结合,因此很难包含在内.让我看看能否澄清一下.每次我运行脚本时,都需要将'agr'的值设置为一个新对象,该对象是在C#中创建的,然后使用scope.SetVariable()在python执行之前创建.某些核心模块被导入并编译到缓存的作用域中.在脚本执行时,使用通过共享范围创建的SharedSymbolDictionary创建新的临时范围(以避免每次导入核心模块),该共享范围执行脚本.

UPDATE FOR CLARIFICATION: Sorry about the confusion, but it's a combination of so much code across C# and python it would be hard to include. Let me see if I can clarify a little. Every time I run a script I need to set the value for 'agr' to a new object which is created in C# prior to python execution using scope.SetVariable(). Some core modules are imported and compiled into a cached scope. On script execution a new temporary scope is created using a SharedSymbolDictionary created with the shared scope (to avoid importing core modules every time) which executes the script.

问题是在主脚本和核心(预编译)脚本中首次都正确设置了"agr",但是在随后的脚本执行中,主脚本中的"agr"是正确的,但是当核心脚本引用了"agr",它指向的是创建第一个执行的"agr",而不是为该执行创建的新的"agr"对象,并且其大多数引用现在为null.

The problem is 'agr' is set correctly the first time both in the main script and the core (precompiled) scripts, however on subsequent script executions 'agr' is correct in the main script, but when the core scripts reference 'agr' it is pointing to the 'agr' created the first execution and NOT the new 'agr' object created for that execution and most of its references are null now.

推荐答案

所以我没有明确的解释,但是我找到了一个简单的解决方案.最初,"agr"是无处不在的变量名... scope.SetVariable(),顶级脚本和预编译的核心脚本中.

So I don't have a solid explanation, but I found a simple solution. Originally 'agr' was the variable name used everywhere... scope.SetVariable(), in the top level scripts, and in the precompiled core scripts.

为解决此问题,我将C#更改为SetVariable()使用变量名"aggregator".然后,我创建了一个由所有顶级主脚本(即sharedModule)导入的模块,然后使用...

For the fix, I changed the C# to use the variable name 'aggregator' for SetVariable(). I then created a module imported by all the top level main scripts i.e. sharedModule and then used...

sharedModule.agr = aggregator

然后,我将所有核心脚本更改为使用sharedModule.agr而不是仅使用'agr',这似乎可以按照我想要的方式工作.

Then I changed all core scripts to use sharedModule.agr instead of just 'agr' and that seems to work the way I want it.

这篇关于为什么不能在IronPython中更改缓存模块中的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆