Git Product home page Git Product logo

Comments (4)

HoustonFortney avatar HoustonFortney commented on June 30, 2024 1

Amazing timing. I actually was looking for the same thing in this library today. I think this would make my code that is consuming this library more readable. It seems to fit the general motivations for context managers outlined in PEP 343.

I made a PR if you are open to adding this. I did not address the multi-thread/process case you mentioned.

from wakepy.

fohrloop avatar fohrloop commented on June 30, 2024

I was thinking this a bit when I did the initial version. I think having a context manager version could make sense. Although, the set_keepawake() and unset_keepawake() are probably also needed:

  • Function approach (set_keepawake() and unset_keepawake()) is easy to understand even for beginners.
  • Function approach makes it possible to set the keepawake on one thread/process, and unset in some other thread/process.

The context manager approach has the positive of being a bit less verbose especially if there is some error handling. Do you see other benefits? It could be implemented as

with keepawake(**params):
    # do stuff

and it could use the set_keepawake() and unset_keepawake() under the hood.

That being said, one day it might be interesting to have multiprocessing-supporting approach that uses a counter under the hood; for each set_keepawake() call there should be unset_keepawake() and for example if one calls set_keepawake() three times, the unset_keepawake() should be called three times before it would actually bet unset. In current implementation, if there were three long running processes (2hrs, 30hrs and 30hrs), and all of them would be using unset_keepawake(), PC would sleep soon after 2hrs.

from wakepy.

fohrloop avatar fohrloop commented on June 30, 2024

Thanks for the PR! The context manager is now part of 0.4.0 and available in PyPI.

from wakepy.

bubblefoil avatar bubblefoil commented on June 30, 2024

Awesome, thanks!

from wakepy.

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.