GithubHelp home page GithubHelp logo

Better Examples Needed about portalocker HOT 3 CLOSED

wolph avatar wolph commented on May 28, 2024
Better Examples Needed

from portalocker.

Comments (3)

wolph avatar wolph commented on May 28, 2024

I agree, it can definitely be improved :)

Also is there anyway to update the lock from one flags to another without unlocking?

Not as far as I am aware, but if you are using a shared lock you could create a new lock in addition. I doubt it's useful that way however...

from portalocker.

elibroftw avatar elibroftw commented on May 28, 2024

Don't worry I ended up using writing to another file and waiting 0.1 seconds in the additional instances, here's what I have done thank to you.

def ensure_single_instance(is_debug=False):
    file = open(LOCK_FILENAME, 'w+', encoding='utf-8')
    # no old running instances found, try locking file
    try:
        # exclusively locked
        portalocker.lock(file, portalocker.LockFlags.EXCLUSIVE | portalocker.LockFlags.NON_BLOCKING)
        create_pid_file()
        if is_debug:
            print(f'Locked {LOCK_FILENAME} pid = {os.getpid()}')
    except LockException:
        # another instance is probably running
        # wait a bit for pid to be written to file
        time.sleep(0.1)
        pid, port = parse_pid_file()
        look_for = 'Music Caster' if IS_FROZEN else 'python'
        # double check if it's already running
        # if more than one instance, there's definitely >3 processes
        threshold = 3 if pid is None else 0
        if is_already_running(threshold=threshold, look_for=look_for, pid=pid):
            if is_debug:
                print('not exiting because we are DEBUGGING')
            else:
                activate_instance(port=port, timeout=5)
                sys.exit()
        else:
            print('instance not found, lock broken?')
    return file

from portalocker.

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.