Giter VIP home page Giter VIP logo

Comments (13)

mathieulongtin avatar mathieulongtin commented on June 29, 2024 1

So it turns out Spark's pickle does fancy things to pickle things pickle can't pickle. (say that fast).

Basic pickle couldn't serialize an attr.Attribute at all and mumbled something about getstate. Sure enough, you can fix the whole thing by adding these methods to Attribute:

      def __getstate__(self):
          """Play nice with pickle"""
          return tuple(getattr(self, a) for a in self.__slots__)

      def __setstate__(self, state):
          """Play nice with pickle"""
          __bound_setattr = _obj_setattr.__get__(self, Attribute)
          for name, value in zip(self.__slots__, state):
              __bound_setattr(name, value)

And it works with Spark.

Basic test:

x = attr.Attribute.from_counting_attr('x', attr.ib())
assert x == pickle.loads(pickle.dumps(x))

This fails with __getstate__ and succeeds with.
I'll send a pull request once I understand your test harness better. I'm guessing this goes in the dark magic section.

It would be a good idea to add this to frozen classes so they work with Pickle.

from attrs.

hynek avatar hynek commented on June 29, 2024

Well because 16.0 didn't have frozen classes? :) Pickle seems to use setattr therefore you can't use it with frozen classes.

from attrs.

mathieulongtin avatar mathieulongtin commented on June 29, 2024

Actually, my code doesn't use frozen. attr.Attribute uses frozen, and that's what breaks. This exact code is working fine with 16.0.0.

from attrs.

hynek avatar hynek commented on June 29, 2024

Oh drat!

from attrs.

Tinche avatar Tinche commented on June 29, 2024

(Commenting on the commit)

Ha, that's very clever. It cuts down attr.Attribute instantiation time by ~45%. However, it slows down access on the Attribute fields, so for example asdict is slower by ~12%. It's an interesting tradeoff, but I'd always trade class definition time (~once per process) for runtime operations like asdict that get called n times.

I wouldn't know about picking attr.Attribute, but pickling instances of non-frozen attr classes in general seems to work fine. (Frozen, not so much, yeah.) Does PySpark require you to pickle classes, as well as instances?

from attrs.

mathieulongtin avatar mathieulongtin commented on June 29, 2024

PySpark is more aggressive about what it pickles so it can support lambda and inline classes.

from attrs.

hynek avatar hynek commented on June 29, 2024

FWIW, I could live with adding PySpark to the test suite to prevent future regressions.

from attrs.

Tinche avatar Tinche commented on June 29, 2024

Just to clarify, my initial comment was to do with changing the Attribute class to have the _values tuple and properties. Adding __getstate__ and __setstate__ to frozen classes basically has no impact. From playing around in the REPL, it even works with nested attrs classes.

What are the exact combinations of parameters that trigger this? slots=True and frozen=True?

from attrs.

mathieulongtin avatar mathieulongtin commented on June 29, 2024

PR done. I added a test to pickle all the classes defined in the dark magic test. It seems to work fine with frozen classes without __getstate__. The problem was only slots classes.

It works fine with Spark, or rather, my previously working Spark code works with this patch.

from attrs.

Tinche avatar Tinche commented on June 29, 2024

Hm, really? On Python 3.5, only both slots=True and frozen=True fails. https://gist.github.com/Tinche/33b702c6a1c3e4093be95953720cd80c

from attrs.

mathieulongtin avatar mathieulongtin commented on June 29, 2024

I test with 2.7. Any slots=True failed. frozen=True, slots=False works fine.

from attrs.

Tinche avatar Tinche commented on June 29, 2024

Hm, can confirm, on 2.7 cases B and C fail.

On 3.5 only C fails.

from attrs.

mathieulongtin avatar mathieulongtin commented on June 29, 2024

Slots seems to be supported by Pickle in Python 3.5 without __getstate__. Anyway, my PR seems to cure all.

from attrs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.