Different behaviour in normal and debug run in threaded Virtualbox - python

I'm encountering a weird error when running the script in PyCharm's debug mode or inside a Flask app. I have narrowed down my code to the following
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()
Running this as a module produces no errors and executes perfectly fine but in debug mode it produces the following error message
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
Going into \vboxapi__init__.py we find
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);
What's causing this error? Does sys.exc_info() behave differently inside a thread inside a debugger?

There seems to be some known, unresolved issues in the interaction between threading and virtualbox, see here. I would suggest using multiprocessing instead as I have not experienced any of the previous issues with it.

Related

Python3.8 generic thread error "Exception ignored in thread started by..."

The program below needs to import os to work correctly.
from threading import Thread
# import os
class Downloader(Thread):
#classmethod
def __init__(self):
super().__init__(self)
#classmethod
def run(self):
os.path.join("aas", "sadas")
def main():
Downloader().start()
if __name__ == "__main__":
main()
In python 3.8, the above program prints a generic error message for any unhandled error occurred in the thread. Same is true for python 3.9 as well.
Exception ignored in thread started by: <bound method Thread._bootstrap of <Downloader(Thread-1, started 123145545740288)>>
Traceback (most recent call last):
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/threading.py", line 890, in _bootstrap
self._bootstrap_inner()
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/threading.py", line 934, in _bootstrap_inner
self._invoke_excepthook(self)
TypeError: invoke_excepthook() takes 1 positional argument but 2 were given
While in python 3.6, it prints the actual error.
Exception in thread Thread-1:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "threads.py", line 10, in run
os.path.join("aas", "sadas")
NameError: name 'os' is not defined
I am having a hard time debugging my threads. The best I can do is to guess the line number and the error. The other posts with the same error deal specifically with the error causing the issue - so not useful.
Why this change in python 3.8? Is there any way to display a proper error message?

How do i override windows permissions for python file windows 10

This is my current code:
import psutil
count = 0
while count < 3000000000:
for process in psutil.process_iter():
if process.name().lower() == 'chrome.exe':
print(process)
process.terminate()
count = count + 1
else:
print('no')
print(count)
its scanning all of he processes running. It then crashes when it scans:
' WindowsInternal.ComposableShell.Experiences.TextInput.Inpu...'
This is the windows service for the on screen keyboard. I tried to stop it running however it still runs. I was wondering if i could give my python file full permissions so this stops happening.
This is the error code from the terminal:
Traceback (most recent call last):
File "C:\Users\benmi\PycharmProjects\HelloWorld\venv\lib\site-packages\psutil_common.py", line 449, in wrapper
ret = self._cache[fun]
AttributeError: _cache
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\benmi\PycharmProjects\HelloWorld\venv\lib\site-packages\psutil_pswindows.py", line 679, in wrapper
return fun(self, *args, **kwargs)
File "C:\Users\benmi\PycharmProjects\HelloWorld\venv\lib\site-packages\psutil_common.py", line 452, in wrapper
return fun(self)
File "C:\Users\benmi\PycharmProjects\HelloWorld\venv\lib\site-packages\psutil_pswindows.py", line 766, in exe
exe = cext.proc_exe(self.pid)
PermissionError: [WinError 24] The program issued a command but the command length is incorrect: '(originated from NtQuerySystemInformation)'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/benmi/PycharmProjects/HelloWorld/TerminateProgram.py", line 5, in
if process.name().lower() == 'chrome.exe':
File "C:\Users\benmi\PycharmProjects\HelloWorld\venv\lib\site-packages\psutil__init__.py", line 630, in name
name = self._proc.name()
File "C:\Users\benmi\PycharmProjects\HelloWorld\venv\lib\site-packages\psutil_pswindows.py", line 750, in name
return os.path.basename(self.exe())
File "C:\Users\benmi\PycharmProjects\HelloWorld\venv\lib\site-packages\psutil_pswindows.py", line 681, in wrapper
raise convert_oserror(err, pid=self.pid, name=self._name)
psutil.AccessDenied: psutil.AccessDenied (pid=8612)

D-Bus - 'ServiceUnknown' exception encountered while calling a remote procedure

I'm trying to call the remote procedure DisplayFolderAndSelect() of Thunar file manager from my own program:
import dbus
bus = dbus.SessionBus()
obj = bus.get_object('org.xfce.Thunar', '/org/xfce/FileManager')
iface = dbus.Interface(obj, 'org.xfce.FileManager')
_thunar_display_folder_and_select = iface.get_dbus_method('DisplayFolderAndSelect')
_thunar_display_folder_and_select('~/Downloads/', 'doc.pdf', '', '')
However I've encountered the following exception at runtime:
Traceback (most recent call last): File "", line 1, in
File "/usr/lib/python2.7/dist-packages/dbus/proxies.py",
line 70, in call
return self._proxy_method(*args, **keywords) File "/usr/lib/python2.7/dist-packages/dbus/proxies.py", line 145, in
call
**keywords) File "/usr/lib/python2.7/dist-packages/dbus/connection.py", line 651, in
call_blocking
message, timeout) dbus.exceptions.DBusException: org.freedesktop.DBus.Error.ServiceUnknown: The name :1.576 was not
provided by any .service files
I'm unable to understand what does this exception mean. And what's the cause behind the exception.
Any thoughts?
I think it is an OS-related issue, restarting D-Bus service solved the problem

How to handle initializer error in multiprocessing.Pool?

When initializer throw Error like below, script won't stop.
I would like to abort before starting main process(do not run 'do_something').
from multiprocessing import Pool
import contextlib
def initializer():
raise Exception("init failed")
def do_something(args):
# main process
pass
pool = Pool(1, initializer=initializer)
with contextlib.closing(pool):
try:
pool.map_async(do_something, [1]).get(100)
except:
pool.terminate()
The never stopping stacktrace on console is below
...
Exception: init failed
Process ForkPoolWorker-18:
Traceback (most recent call last):
File "/home/hoge/anaconda3/lib/python3.6/multiprocessing/process.py", line 249, in _bootstrap
self.run()
File "/home/hoge/anaconda3/lib/python3.6/multiprocessing/process.py", line 93, in run
self._target(*self._args, **self._kwargs)
File "/home/hoge/anaconda3/lib/python3.6/multiprocessing/pool.py", line 103, in worker
initializer(*initargs)
File "hoge.py", line 5, in initializer
raise Exception("init failed")
Exception: init failed
...
My workaround is suppressing initializer error and return at the beginning of the main process by using global flag like below.
But I would like to learn better one.
def initializer():
try:
raise Exception("init failed")
except:
global failed
failed = True
def do_something(args):
global failed
if failed:
# skip when initializer failed
return
# main process
After navigating through the implementation of multiprocessing using PyCharm, I'm convinced that there is no better solution, because Pool started a thread to _maintain_pool() by _repopulate_pool() if any worker process exists--either accidentally or failed to initialize.
Check this out: Lib/multiprocessing/pool.py line 244
I just came across the same woe. My first solution was to catch the exception and raise it in the worker function (see below). But on second thought it really means that initializer support of multiprocessing.Pool is broken and sould not be used. So I now prefer to do the initialization stuff directly in the worker.
from multiprocessing import Pool
import contextlib, sys
_already_inited = False
def initializer():
global _already_inited
if _already_inited:
return
_already_inited = True
raise Exception("init failed")
def do_something(args):
initializer()
# main process
pool = Pool(1)
with contextlib.closing(pool):
pool.map_async(do_something, [1]).get(100)
Both the code and the stacktrace are simpler.
Off course all your worker function need to call initializer().
My initial solution was to defer the exception to the worker function.
from multiprocessing import Pool
import contextlib, sys
failed = None
def initializer():
try:
raise Exception("init failed")
except:
global failed
failed = sys.exc_info()[1]
def do_something(args):
global failed
if failed is not None:
raise RuntimeError(failed) from failed
# main process
pool = Pool(1, initializer=initializer)
with contextlib.closing(pool):
pool.map_async(do_something, [1]).get(100)
That way the caller still gets access to the exception.
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "/usr/lib/python3.5/multiprocessing/pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "/usr/lib/python3.5/multiprocessing/pool.py", line 44, in mapstar
return list(map(*args))
File "/tmp/try.py", line 15, in do_something
raise RuntimeError(failed)
RuntimeError: init failed
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/tmp/try.py", line 20, in <module>
pool.map_async(do_something, [1]).get(100)
File "/usr/lib/python3.5/multiprocessing/pool.py", line 608, in get
raise self._value
RuntimeError: init failed
(venv) kmkaplan#dev1:~/src/options$ python3 /tmp/try.py
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "/tmp/try.py", line 7, in initializer
raise Exception("init failed")
Exception: init failed
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/lib/python3.5/multiprocessing/pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "/usr/lib/python3.5/multiprocessing/pool.py", line 44, in mapstar
return list(map(*args))
File "/tmp/try.py", line 15, in do_something
raise RuntimeError(failed) from failed
RuntimeError: init failed
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/tmp/try.py", line 20, in <module>
pool.map_async(do_something, [1]).get(100)
File "/usr/lib/python3.5/multiprocessing/pool.py", line 608, in get
raise self._value
RuntimeError: init failed

Transferring code from pygst to pyGi

I wanted to build a program which streams radio from address. I wrote the following code:
try:
from gi.repository import Gst
except AttributeError as e:
pass
def on_tag(bus, msg):
taglist = msg.parse_tag()
print 'on_tag:'
for key in taglist.keys():
print '\t%s = %s' % (key, taglist[key])
music_stream_uri = 'http://213.8.143.168/91fmAudio'
player = Gst.element_factory_make("playbin", "player")
player.set_property('uri', music_stream_uri)
player.set_state(Gst.STATE_PLAYING)
It worked well when I did
import pygst
pygst.require("0.10")
import gst
but since It gave me several AttributeError and warning claims the module is deprecating I switched to pyGi and imported
from pi.repository import Gst
but then I got the error
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.1\helpers\pydev\pydevd.py", line 2222, in <module>
globals = debugger.run(setup['file'], None, None)
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.1\helpers\pydev\pydevd.py", line 1648, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:/Danis/radio/radio.py", line 20, in <module>
player = Gst.element_factory_make("playbin", "player")
File "C:\Python27\lib\site-packages\gi\module.py", line 320, in __getattr__
return getattr(self._introspection_module, name)
File "C:\Python27\lib\site-packages\gi\module.py", line 139, in __getattr__
self.__name__, name))
AttributeError: 'gi.repository.Gst' object has no attribute 'element_factory_make'
I wonder which command in pi.Gst is equivalent to the commands of the pygst? Does anybody know how to transfer this piece of code to PyGi?
I recommend you install an interpreter with tab completion such as ipython to look up methods. You will find documentation for introspected libs in python at http://lazka.github.io/pgi-docs/
Regarding your specific problem, the call to make is Gst.ElementFactory.make ()
Cheers!

Categories