1

我想GdkPixbuf.Pixbuf在 Python3 中腌制和解开 a 。更具体地说multiprocessing,Python3 的包需要这样做,因为我通过Queue.

问题是对象从

<GdkPixbuf.Pixbuf object at 0x7f8b9e9cfb88 (GdkPixbuf at 0x563b61725c60)>

<GdkPixbuf.Pixbuf object at 0x7f8b9e9eaea0 (uninitialized at 0x(nil))>

这是最小的工作示例。

>>> import gi
>>> from gi.repository import GdkPixbuf
__main__:1: PyGIWarning: GdkPixbuf was imported without specifying a version first. Use gi.require_version('GdkPixbuf', '2.0') before import to ensure that the right version gets loaded.

>>> pf = GdkPixbuf.Pixbuf.new_from_file('_icon.png')
>>> pf
<GdkPixbuf.Pixbuf object at 0x7f8b9e9cfb88 (GdkPixbuf at 0x563b61725c60)>

>>> import pickle
>>> pickle.dump(pf, open('p', 'wb'))

>>> pb2 = pickle.load(open('p', 'rb'))
>>> pb2
<GdkPixbuf.Pixbuf object at 0x7f8b9e9eaea0 (uninitialized at 0x(nil))>

我看不到其他腌制方法。图标需要在单独的进程中加载​​(在不同的 CPU 内核上,然后是应用程序主/第一个进程),然后应传输到主进程。这是通过Queue腌制所有数据来完成的。

4

1 回答 1

0

我的解决方案不是将“图标”作为Pixbuf内存中的对象,而是作为我从文件中读取的原始字节。

解开这些字节后,我将它们转换为Pixbuf.

>>> import gi
>>> from gi.repository import GdkPixbuf, Gio, GLib
__main__:1: PyGIWarning: GdkPixbuf was imported without specifying a version first. Use gi.require_version('GdkPixbuf', '2.0') before import to ensure that the right version gets loaded.
>>> with open('_icon.png', 'rb') as f:
...     icon_bytes = f.read()
... 
>>> 
>>> import pickle
>>> pickle.dump(icon_bytes, open('p', 'wb'))
>>> 
>>> pb = pickle.load(open('p', 'rb'))
>>> pb = GLib.Bytes(pb)
>>> pb = GdkPixbuf.Pixbuf.new_from_stream(Gio.MemoryInputStream.new_from_bytes(pb))
>>> pb
<GdkPixbuf.Pixbuf object at 0x7fc0858ac5e8 (GdkPixbuf at 0x55e0d8d08b60)>
于 2019-05-21T21:15:44.657 回答