简体   繁体   English

在金字塔中使用NumPy

[英]Using NumPy in Pyramid

I'd like to perform some array calculations using NumPy for a view callable in Pyramid. 我想使用NumPy对可在金字塔中调用的视图执行一些数组计算。 The array I'm using is quite large (3500x3500), so I'm wondering where the best place to load it is for repeated use. 我正在使用的阵列很大(3500x3500),所以我想知道加载它的最佳位置是重复使用。

Right now my application is a single page and I am using a single view callable. 现在,我的应用程序是一个页面,并且我使用的是可调用的单个视图。

The array will be loaded from disk and will not change. 该阵列将从磁盘加载,并且不会更改。

If the array is something that can be shared between threads then you can store it in the registry at application startup ( config.registry['my_big_array'] = ?? ). 如果数组可以在线程之间共享,则可以在应用程序启动时将其存储在注册表中( config.registry['my_big_array'] = ?? )。 If it cannot be shared then I'd suggest using a queuing system with workers that can always have the data loaded, probably in another process. 如果不能共享它,那么我建议对可能总是在另一个进程中加载​​数据的工作人员使用排队系统。 You can hack this by making the value in the registry be a threadlocal and then storing a new array in the variable if one is not there already, but then you will have a copy of the array per thread and that's really not a great idea for something that large. 您可以通过将注册表中的值设置为threadlocal,然后将新数组存储在变量中(如果尚未存在的话)来破解它,但是每个线程将拥有该数组的副本,对于这么大的东西。

I would just load it in the obvious place in the code, where you need to use it (in your view, I guess?) and see if you have performance problems. 我只是将其加载到代码中明显的位置,您需要在其中使用它(在您看来,我认为呢?),看看是否存在性能问题。 It's better to work with actual numbers than try to guess what's going to be a problem. 与实际数字一起工作比尝试猜测将要出现的问题要好。 You'll usually be surprised by the reality. 您通常会对现实感到惊讶。

If you do see performance problems, assuming you don't need a copy for each of multiple threads, try just loading it in the global scope after your imports. 如果确实看到性能问题,则假设不需要多个线程中的每个线程的副本,请尝试在导入后将其加载到全局范围中。 If that doesn't work, try moving it into its own module and importing that. 如果那不起作用,请尝试将其移至其自己的模块中并导入。 If that still doesn't help... I don't know what then. 如果那仍然没有帮助...我不知道那怎么办。

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

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