GNOME Bugzilla – Bug 791265
Unable to pickle GLib.Error
Last modified: 2018-04-05 16:18:52 UTC
It's not possible to pickle GLib.Error instances. This bit me hard in a specific situation where I was returning exceptions from a multiprocessing.Subprocess() using a queue. When the exception was a GLib.Error instance, my attempt to return the exception would silently fail and the subprocess would appear to have died for no reason.
Example of failure to pickle: >>> from gi.repository import GLib >>> import pickle >>> pickle.dumps(GLib.Error("Example")) Traceback (most recent call last):
+ Trace 238226
Thanks. Can you provide a small example showing the problem with subprocess and queue?
Created attachment 365030 [details] Example showing my attempt to return an exception from a 'multiprocessing' subprocess via a queue Sure, here's an example -- slightly contrived but shows a scenario where I would want to be able to pass a GLib.Error from subprocess to a main process, and demonstrates that it doesn't work. It's contrived in part because the main process doesn't try to handle the exception, so it could be "fixed" by just printing the traceback in the subprocess. But there are many cases where you want to do more with the exception in the main process than to just re-raise it, and none of that is easily possible if we can't get the exception into a Queue object.
Thanks, to summarize: * Using multiprocessing.Queue one can return exception instances to the parent process. * Normal Python exceptions are picklable * GLib.Error is an "exception" and is not picklable Sounds reasonable. ---- import multiprocessing from gi.repository import GLib def add_queue(e): q = multiprocessing.Queue() q.put(e) print(repr(q.get())) try: raise ValueError("foo") except Exception as e: add_queue(e) try: GLib.file_get_contents("") except Exception as e: add_queue(e)
Thanks for summarizing a bit more clearly than I could :-)
-- GitLab Migration Automatic Message -- This bug has been migrated to GNOME's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/pygobject/issues/145.
https://gitlab.gnome.org/GNOME/pygobject/merge_requests/54