Python:subprocess.Popen()的第一个实例非常慢 [英] Python: first instance of subprocess.Popen() is very slow
问题描述
我确定我缺少一些简单的东西,但是在使用子流程模块时,要启动第一个子流程要花很长时间(> 10秒).第二个开始在第一个之后不久.有没有什么办法解决这一问题?下面的代码:
I'm sure I'm missing something simple, but when using the subprocess module, there is a very significant wait (> 10 seconds) to starting the first subprocess. The second one starts shortly after the first. Is there any way to fix this? Code below:
要添加,HWAccess(在proc.py中)链接一个dll.这可能与它有关吗? 我将测试归结为开始一个SINGLE子进程,与直接从cmd提示符下直接运行proc.py相比,导入HWAccess所花费的时间要长得多.我看不出这与dll有什么关系,特别是如果它从cmd快速加载,但不是通过test.py作为子进程的话.
To add, HWAccess (in proc.py) links a dll. Could this have anything to do with it? I've boiled the test down to starting a SINGLE subprocess and it takes significantly longer to import HWAccess than if I just run proc.py directly from cmd prompt. I don't see how this has anything to do with the dll specifically if it loads fast from cmd, but not as a sub-process through test.py
test.py:
import subprocess
import os
import time
print 'STARTING'
proc0 = subprocess.Popen(['python','proc.py','0'])
proc1 = subprocess.Popen(['python','proc.py','1'])
while True:
try: pass
except KeyboardInterrupt:
os._exit(0)
except ValueError:
pass
proc.py:
print 'Process starting...'
import HWAccess
print 'HWAccess imported...'
import sys
print 'sys imported...'
import time
print 'time imported...'
print 'hi from ',sys.argv[1]
放入打印件之后,大约需要5秒钟才能到达第一个处理开始...",第二个处理之后立即打印处理开始...".然后会有大约30秒的暂停来导入HWAccess(在单个进程上运行仅需几秒钟),然后第二个进程立即打印出它也已经导入了HWAccess ...从那时起执行速度很快. HWAccess链接了一个.dll,所以我想知道两个试图导入HWAccess的进程是否会导致某种竞争状态,而这需要花费一些时间进行协商.
After putting the prints in, there is around 5s to reach the first 'Process starting...', the second process prints 'Process starting...' immediately afterwards. Then there is a ~30 second pause to import HWAccess (takes a matter of seconds running on an individual process), the second process then immediately prints that it too has imported HWAccess... from then on execution is fast. HWAccess links a .dll so I'm wondering if two processes trying to import HWAccess result in some sort of race condition that takes a while to negotiate.
推荐答案
我不确定这是否是正确的方法,但是我记得在启动一个进程时会看到这样的延迟(而且一点也不与Python相关),并且原来,它们与我计算机上某些配置错误的网络设置有关.在子进程启动时,它必须建立进程间通信,并且这些设置可能会干扰.
I am not sure if this is the right track, but I remember seeing such delays when starting a process wayyy back (and not at all Python related), and it turned out they were related to some badly configured network settings on my computer. Upon subprocess start-up, it has to set up interprocess communication, and those settings might interfere.
我记得我的问题与在计算机上使用错误的主机名有关,该主机名在网络上配置不正确-您可以检查是否属于您的情况吗?如果不是生产计算机,请尝试完全不设置主机名,而将其保留为"localhost".
I remember my problems were related to using a false hostname for the machine, which was not properly configured on the network - can you check to see if it is your case? If it is not a production machine, try not setting a hostname at all, leaving it as "localhost".
这篇关于Python:subprocess.Popen()的第一个实例非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!