Giter Site home page Giter Site logo

Multithreading about gutter HOT 9 OPEN

teeberg avatar teeberg commented on August 24, 2024
Multithreading

from gutter.

Comments (9)

Fluxx avatar Fluxx commented on August 24, 2024

Hi @teeberg,

Gutter is backed by durabledict, which should give you a consistent[1] view of switches in your data store, even if there is a copy of the manger per thread. Was there a specific issue or some other problem you were running into that was unexpected with multiple threads.

[1] Obviously the memory backend will not be consistent, but the Redis and ZooKeeper ones should be consistent enough no problem.

from gutter.

teeberg avatar teeberg commented on August 24, 2024

Hi @Fluxx, thanks for the fast answer! My understanding of gutter was clearly not good enough to correctly identify the problem. :-) We are in fact using a memory backend in our test environment. When we settled for it, we didn't worry about multiple threads. I also didn't realise this would cause a problem. Thanks for clarifying!

from gutter.

teeberg avatar teeberg commented on August 24, 2024

I've debugged some more and tracked down what I think is the real problem: What really doesn't work in the two-thread scenario is the SwitchContextManager from gutter.client.testutils. It temporarily overrides gutter.active, but because the gutter Manager is thread-local, the other thread doesn't pick up this change. Making the Manager inherit from object instead of threading.local fixes the problem, but that can't be the solution. :-)

from gutter.

Fluxx avatar Fluxx commented on August 24, 2024

Ah yeah. That's a limitation of SwitchContextManager since, as you mentioned, it monkey patches gutter.active.

IIRC (as I wrote that code over 3 years ago), the intention with making the Manager a threading.local was to prevent people from shooting themselves in the foot by sharing a Manager instance as a singleton between threads and causing problems. I still agree with that sentiment I think, but I do hear you that your use case of the Django LiveServerTestCase would be nice to have thread communication on the Manager state.

I think what might be best in this scenario is to change the class hierarchy a little to have a base somewhat private BaseManager class not inherit from theading.local, and instead make the canonical Manager class inherit from (BaseManager, threading.local). Thus for most users they can just use the canonical Manager class, but for users who need to some more savvy thread coordination they can use a custom manager they inherit from BaseManager.

Thoughts?

from gutter.

teeberg avatar teeberg commented on August 24, 2024

I think it's reasonable to make it thread-local by default. It would of course be nice if the Manager was fully thread-safe but I'm not sure how hard that would be to achieve.

Leaving that out, what you're suggesting sounds like a quick solution that I could work with! It would be awesome if you could make this change here! Maybe other users can benefit from it too, then :-)

from gutter.

Fluxx avatar Fluxx commented on August 24, 2024

It would be awesome if you could make this change here! Maybe other users can benefit from it too, then :-)

Yeah I can totally do that. Won't be today though, but by the weekend for sure.

from gutter.

teeberg avatar teeberg commented on August 24, 2024

Awesome, I'm looking forward to that change! Thank you so much for your efforts!

from gutter.

teeberg avatar teeberg commented on August 24, 2024

How do you feel about a patch like this one? teeberg@b887062

from gutter.

thekashifmalik avatar thekashifmalik commented on August 24, 2024

That seems like a good idea. I'll be taking a look at this repo soon.

from gutter.

Related Issues (16)

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.