简体   繁体   English

Python:subprocess.Popen()的第一个实例非常慢

[英]Python: first instance of subprocess.Popen() is very slow

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. 我确定我缺少一些简单的东西,但是在使用子流程模块时,要启动第一个子流程的等待时间非常长(> 10秒)。 The second one starts shortly after the first. 第二个开始在第一个之后不久。 Is there any way to fix this? 有没有什么办法解决这一问题? Code below: 代码如下:

EDIT: To add, HWAccess (in proc.py) links a dll. 编辑:要添加,HWAccess(在proc.py中)链接一个dll。 Could this have anything to do with it? 这可能与它有关吗? EDIT2: 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. EDIT2:我将测试归结为启动一个SINGLE子进程,与直接从cmd提示符下直接运行proc.py相比,导入HWAccess所需的时间明显更长。 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 我看不到这与dll有什么关系,特别是如果它从cmd快速加载,但不是通过test.py作为子进程的话

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: 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]

Edit: After putting the prints in, there is around 5s to reach the first 'Process starting...', the second process prints 'Process starting...' immediately afterwards. 编辑:放入打印件后,大约需要5秒钟才能到达第一个“ Process starts ...”,第二个进程随后立即打印“ Process starting ...”。 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. 然后会有大约30秒的暂停来导入HWAccess(在单个进程上运行仅需几秒钟),然后第二个进程立即打印出它也已经导入了HWAccess ...从那时起执行速度很快。 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. HWAccess链接了一个.dll,所以我想知道两个尝试导入HWAccess的进程是否会导致某种竞争状态,而这需要一段时间才能进行协商。

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. 我不确定这是否是正确的方法,但是我还记得启动进程时出现这样的延迟(而且与Python完全无关),事实证明,这些延迟与我计算机上某些错误的网络设置有关。 Upon subprocess start-up, it has to set up interprocess communication, and those settings might interfere. 在子进程启动时,它必须建立进程间通信,并且这些设置可能会干扰。

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". 如果不是生产计算机,请尝试完全不设置主机名,而将其保留为“ localhost”。

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

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