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

Commands can't be defined in __main__ module #837

Open
vringar opened this issue Dec 23, 2020 · 3 comments
Open

Commands can't be defined in __main__ module #837

vringar opened this issue Dec 23, 2020 · 3 comments
Labels
backlog Not a priorty, but nice to have bug

Comments

@vringar
Copy link
Contributor

vringar commented Dec 23, 2020

Due to limitations in dill, classes defined in the __main__ module can't be pickled.
Since we send all of our commands through a multiprocess.queue which uses dill internally we can't send Commands defined in the main module.

See uqfoundation/multiprocess#22 and uqfoundation/multiprocess#5 for progress on allowing us to specify a different serializer in multiprocess.

@vringar vringar added backlog Not a priorty, but nice to have bug labels Dec 23, 2020
@mmckerns
Copy link

Due to limitations in dill, classes defined in the __main__ module can't be pickled.

This isn't true.

Python 3.7.14 (default, Sep 10 2022, 11:17:06) 
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dill
>>> class Foo(object):
...   def __init__(self, x):
...     self.x = x
...   def bar(self, y):
...     return self.x + y
... 
>>> f = Foo(1)
>>> 
>>> import multiprocess as mp
>>> mp.Pool().map(f.bar, [1,2,3,4])
[2, 3, 4, 5]
>>> dill.loads(dill.dumps(f)).bar(1)
2
>>> dill.loads(dill.dumps(Foo))(1).bar(1)
2

Can you be more specific? If you are interested in abc classes and methods, some can be handled and some can't. dill currently has issues with _abc_data objects, but there's a PR that is pending for that.

@vringar
Copy link
Contributor Author

vringar commented Oct 9, 2022

If I copy the contents of LinkCountingCommand over to the demo.py and run python demo.py, I get the following error:

Traceback (most recent call last):
  File "/home/stefan/.conda/envs/openwpm/lib/python3.10/site-packages/multiprocess/queues.py", line 248, in _feed
    obj = _ForkingPickler.dumps(obj)
  File "/home/stefan/.conda/envs/openwpm/lib/python3.10/site-packages/multiprocess/reduction.py", line 54, in dumps
    cls(buf, protocol, *args, **kwds).dump(obj)
  File "/home/stefan/.conda/envs/openwpm/lib/python3.10/site-packages/dill/_dill.py", line 620, in dump
    StockPickler.dump(self, obj)
  File "/home/stefan/.conda/envs/openwpm/lib/python3.10/pickle.py", line 487, in dump
    self.save(obj)
  File "/home/stefan/.conda/envs/openwpm/lib/python3.10/pickle.py", line 603, in save
    self.save_reduce(obj=obj, *rv)
  File "/home/stefan/.conda/envs/openwpm/lib/python3.10/pickle.py", line 687, in save_reduce
    save(cls)
  File "/home/stefan/.conda/envs/openwpm/lib/python3.10/pickle.py", line 560, in save
    f(self, obj)  # Call unbound method with explicit self
  File "/home/stefan/.conda/envs/openwpm/lib/python3.10/site-packages/dill/_dill.py", line 1838, in save_type
    _save_with_postproc(pickler, (_create_type, (
  File "/home/stefan/.conda/envs/openwpm/lib/python3.10/site-packages/dill/_dill.py", line 1140, in _save_with_postproc
    pickler.save_reduce(*reduction, obj=obj)
  File "/home/stefan/.conda/envs/openwpm/lib/python3.10/pickle.py", line 692, in save_reduce
    save(args)
  File "/home/stefan/.conda/envs/openwpm/lib/python3.10/pickle.py", line 560, in save
    f(self, obj)  # Call unbound method with explicit self
  File "/home/stefan/.conda/envs/openwpm/lib/python3.10/pickle.py", line 902, in save_tuple
    save(element)
  File "/home/stefan/.conda/envs/openwpm/lib/python3.10/pickle.py", line 560, in save
    f(self, obj)  # Call unbound method with explicit self
  File "/home/stefan/.conda/envs/openwpm/lib/python3.10/site-packages/dill/_dill.py", line 1251, in save_module_dict
    StockPickler.save_dict(pickler, obj)
  File "/home/stefan/.conda/envs/openwpm/lib/python3.10/pickle.py", line 972, in save_dict
    self._batch_setitems(obj.items())
  File "/home/stefan/.conda/envs/openwpm/lib/python3.10/pickle.py", line 998, in _batch_setitems
    save(v)
  File "/home/stefan/.conda/envs/openwpm/lib/python3.10/pickle.py", line 578, in save
    rv = reduce(self.proto)
TypeError: cannot pickle '_abc._abc_data' object

So yeah, I guess we are blocked on the _abc_data PR. Can you link that PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backlog Not a priorty, but nice to have bug
Projects
None yet
Development

No branches or pull requests

3 participants