在不同的文件中导入相同的模块(Importing the same modules in different files)
假设我编写了一组要在python文件中使用的类,并在脚本中使用它们(或者在不同的文件中使用python代码)。 现在这两个文件都需要导入一组模块。 导入是仅包含一次还是包含在两个文件中?
文件1: my_module.py 。
import os class myclass(object): def __init__(self,PATH): self.list_of_directories = os.listdir(PATH)
文件2:
import os import my_module my_module.m = myclass("C:\\User\\John\\Desktop") list_ = m.list_of_directories print os.getcwd()
我应该将行
import os
添加到这两个文件中吗?假设有很多模块需要导入,这对性能有何影响? 另外,一个模块,一旦导入,在这种情况下重新加载?
Supposing I have written a set of classes to be used in a python file and use them in a script (or python code in a different file). Now both the files require a set of modules to be imported. Should the import be included only once, or in both the files ?
File 1 : my_module.py.
import os class myclass(object): def __init__(self,PATH): self.list_of_directories = os.listdir(PATH)
File 2 :
import os import my_module my_module.m = myclass("C:\\User\\John\\Desktop") list_ = m.list_of_directories print os.getcwd()
Should I be adding the line
import os
to both the files ?How does this impact the performance, supposing there are lots of modules to be imported ? Also, is a module ,once imported, reloaded in this case ?
原文:https://stackoverflow.com/questions/30944983
最满意答案
您正在创建一个新计时器
System.Timers.Timer timer = new System.Timers.Timer();
里面有本地范围
protected override void OnStart(string[] args)
当它超出范围时,它可以被垃圾收集。 这可能迟早会发生,因此根据收集垃圾的时间,您的服务会更短或更长。
将计时器声明为与服务一样长的字段:
System.Timers.Timer _timer; protected override void OnStart(string[] args) { [...] _timer = new System.Timers.Timer(); _timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimer); _timer.Start(); [...]
You are creating a new timer
System.Timers.Timer timer = new System.Timers.Timer();
that has a local scope inside
protected override void OnStart(string[] args)
When it goes out of scope, it can be garbage collected. This may happen sooner or later, so your service works shorter or longer depending on when garbage is collected.
Declare the timer as a field that lives as long as the service:
System.Timers.Timer _timer; protected override void OnStart(string[] args) { [...] _timer = new System.Timers.Timer(); _timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimer); _timer.Start(); [...]
相关问答
更多-
你正在把你的类型参数放在错误的地方。 它在HashMap和() : config1 = new HashMap
>>(); 另外,在声明中使用更通用的类型而不是特定的类型,甚至是generic type parameters都是一个好主意。 所以你应该在声明中使用Map而不是HashMap ,而在你的type parameter List而不是ArrayList : - 实际上,你不需要在两行中打破你的声明和初始化。 只需将它们 ... -
我的EXE本身运行良好,但在由服务加载时不做任何事情(My exe runs fine by itself, but does nothing when loaded by a service)[2022-10-05]
在Vista和Windows 2008上,服务在与用户不同的会话中运行 - 任何运行的EXE都将在与服务相同的会话中运行。 在Vista之前,您需要选中“允许服务与桌面交互”框,否则同样的事情适用。 这意味着您的托盘图标EXE无法与用户的桌面进行交互。 您需要查看使用CreateProcessAsUser在正确的会话中运行EXE。 此博客文章针对使用ConfigMgr操作系统部署的人员,但其中包含了在另一个会话中运行进程所需步骤的良好列表。 有一些非显而易见的步骤需要采取或以奇怪的方式失败。 On Vist ... -
无法用ProcessBuilder运行程序,从命令行运行良好(Can't run program with ProcessBuilder, runs fine from command line)[2021-07-06]
看起来通配符不是使用glob扩展的。 您可以改为使用shell: ProcessBuilder pb = new ProcessBuilder("bash", "-c", "java -jar ~/myjar.jar \".*\""); 或者您可以删除通配符周围的双引号: ProcessBuilder pb = new ProcessBuilder(java, "-jar", "~/myjar.jar", ".*"); Looks like the wildcard character ... -
使用Google服务的LocationManager类检查GPS状态: - compile 'com.google.android.gms:play-services:9.8.0' // GPS的演示课程 import android.Manifest; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content. ...
-
您正在创建一个新计时器 System.Timers.Timer timer = new System.Timers.Timer(); 里面有本地范围 protected override void OnStart(string[] args) 当它超出范围时,它可以被垃圾收集。 这可能迟早会发生,因此根据收集垃圾的时间,您的服务会更短或更长。 将计时器声明为与服务一样长的字段: System.Timers.Timer _timer; protected override void OnStart(st ...
-
可能是Heroku使用的Node版本的问题。 您可以尝试将Heroku设置为使用本地使用的Heroku 。 此外,在部署到Heroku之前,您不需要执行npm install ,因为在部署时它将为您提供(因此不要在git上包含node_module文件夹)。 Could be a problem with the Node version that Heroku is using. You could try setting Heroku to use the same one you use locall ...
-
我曾经遇到过同样的问题。 正如我记得的那样,我曾经在war文件中添加了一些依赖项。 本地服务器在其类路径上有jar,但是appengine服务器没有,因此我得到了ClassNotFoundException。 所以你可能需要检查战争中包含哪些罐子。 I had the same problem once. As I remembered it I had forgotton to add some dependencies in the war file. The local server had the ...
-
我的质量设置存在问题。 我已经将v-sync设置为其最高设置,该设置在构建为独立时滞后但在编辑器中未激活。 我将质量设置设置为默认设置,现在一切正常。 There was an issue with my quality settings. I had set v-sync to its highest setting which was lagging the when built as a standalone but wasn't active while in the editor. I set m ...
-
Gradle在交互式帐户下运行正常,但不在NT服务下运行(Gradle runs fine under interactive account but not under NT service)[2022-02-16]
当作为交互式用户成功执行但在作为服务运行时失败的程序的常见问题源是执行周围的用户设置已更改。 例如,当您运行Gradle时,%USERPROFILE%\ .gradle中的设置(通常为c:\ users \ <用户名>,您以交互方式登录的设置与运行该服务的帐户不同。) 当您作为服务运行时,会有一个不同的用户配置文件,或者可能根本没有。 让批处理脚本转储环境(在.bat文件的顶部附近添加“set” - 这将转储按字母顺序排列的环境变量列表),并将输出作为服务运行时的输出与交互运行时的输出进行比较。 查看是否已 ... -
除了@Paul Tomblin所指出的,你假设typedef int Uint32 ,这是不可移植的,也是一种不好的做法。 同时考虑到C ++风格, int color; color = SDL_MapRGB(screen->format, r, g, b); int *pixel; pixel = (int*) screen->pixels + x + y; *pixel = color; 应该是: Uint32* pixels = reinterpret_cast
(screen-> ...