在正常情况下和在线程化Virtualbox中运行调试时,行为不同

时间:2018-06-27 07:45:13

标签: python multithreading flask pycharm virtualbox

在PyCharm的调试模式下或在Flask应用程序中运行脚本时遇到奇怪的错误。我将代码范围缩小到以下

import virtualbox
import threading

class ThreadExecutor(threading.Thread):
    def __init__(self):
        super().__init__()

    def run(self):
        vbox = virtualbox.VirtualBox()


if __name__ == '__main__':
    th = ThreadExecutor()
    th.start()

将其作为模块运行不会产生任何错误,并且执行得很好,但是在调试模式下会产生以下错误消息

Connected to pydev debugger (build 181.5087.37)
Exception in thread Thread-6:
Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\site-packages\vboxapi\__init__.py", line 449, in __init__
    None)
pywintypes.com_error: (-2147221008, 'CoInitialize has not been called.', None, None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:/Users/.../dev/debug.py", line 13, in run
    vbox = virtualbox.VirtualBox()
  File "C:\Program Files\Python36\lib\site-packages\virtualbox\library_ext\vbox.py", line 22, in __init__
    manager = virtualbox.Manager()
  File "C:\Program Files\Python36\lib\site-packages\virtualbox\__init__.py", line 143, in __init__
    self.manager = vboxapi.VirtualBoxManager(mtype, mparams)
  File "C:\Program Files\Python36\lib\site-packages\vboxapi\__init__.py", line 991, in __init__
    self.platform = PlatformMSCOM(dPlatformParams)
  File "C:\Program Files\Python36\lib\site-packages\vboxapi\__init__.py", line 455, in __init__
    print("Warning: CoInitializeSecurity failed: ", oXctp);
NameError: name 'oXctp' is not defined

进入我们找到的\ vboxapi__init __。py

try:
    pythoncom.CoInitializeSecurity(None,
                                   None,
                                   None,
                                   pythoncom.RPC_C_AUTHN_LEVEL_DEFAULT,
                                   pythoncom.RPC_C_IMP_LEVEL_IMPERSONATE,
                                   None,
                                   pythoncom.EOAC_NONE,
                                   None)
except:
    _, oXcpt, _ = sys.exc_info();
    if isinstance(oXcpt, pythoncom.com_error) and self.xcptGetStatus(oXcpt) == -2147417831: # RPC_E_TOO_LATE
        print("Warning: CoInitializeSecurity was already called");
    else:
        print("Warning: CoInitializeSecurity failed: ", oXctp);

是什么导致此错误? sys.exc_info()在调试器中的线程内的行为是否有所不同?

1 个答案:

答案 0 :(得分:0)

线程和virtualbox之间的交互似乎存在一些尚未解决的已知问题,请参见here。我建议改用多处理,因为我以前没有遇到过任何问题。