Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing attribute after using multiprocessing Queue #32

Open
smuecke opened this issue Sep 24, 2020 · 2 comments
Open

Missing attribute after using multiprocessing Queue #32

smuecke opened this issue Sep 24, 2020 · 2 comments

Comments

@smuecke
Copy link

smuecke commented Sep 24, 2020

When using multiprocessing's Queue class to share PCBO instances between processes, the attribute _degree is missing after extraction. A minimal example:

import multiprocessing as mp
from qubovert import PCBO

def produce(queue):
    pcbo = PCBO({(0,1): -5})
    queue.put(pcbo)

def consume(queue):
    pcbo = queue.get() # AttributeError occurs here
    print(pcbo)

if __name__ == '__main__':
    queue = mp.Queue()

    process1 = mp.Process(target=produce, args=(queue,))
    process2 = mp.Process(target=consume, args=(queue,))

    process1.start()
    process2.start()

    process1.join()
    process2.join()

Executing this produces the following error:

Process Process-2:
Traceback (most recent call last):
  File "/usr/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap
    self.run()
  File "/usr/lib/python3.8/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "test.py", line 9, in consume
    pcbo = queue.get()
  File "/usr/lib/python3.8/multiprocessing/queues.py", line 116, in get
    return _ForkingPickler.loads(res)
  File "/home/muecke/.local/lib/python3.8/site-packages/qubovert/utils/_bo_parentclass.py", line 219, in __setitem__
    super().__setitem__(key, value)
  File "/home/muecke/.local/lib/python3.8/site-packages/qubovert/utils/_pubomatrix.py", line 384, in __setitem__
    self._degree = max(self._degree, len(k))
AttributeError: 'PCBO' object has no attribute '_degree'

OS: Linux (Manjaro, kernel 5.7.9-1-MANJARO)
Python version 3.8.3, qubovert version 1.2.3

Thanks in advance!

@jtiosue
Copy link
Owner

jtiosue commented Oct 15, 2020

Hi, thanks for raising the issue! I'm afraid I'm not very familiar with the multiprocessing package or Queue object. Have you been able to reproduce this error in another way?

@smuecke
Copy link
Author

smuecke commented Oct 16, 2020

This seems to be an issue with serialization, as I was able to reproduce it with pickle:

import pickle
from qubovert import PCBO

pcbo = PCBO({(0, 1): -5}) # error happens only when instance is not empty
data = pickle.dumps(pcbo)
pcbo_ = pickle.loads(data) # AttributeError occurs here

The clean solution will probably be to implement the __getstate__() and __setstate__() methods for all instances of PCBO, QUBO etc., which store and restore the object states manually. I think attributes like _degree starting with an underscore are problematic for serialization, because Python renames such attributes in subclasses.
Another solution might be to just rename attributes starting with _ to something without underscore.

Best regards ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants