Esempio n. 1
0
def connect_to_zlock_server():
    # setup connection with the zprocess.locking server, depending on labconfig settings:
    config = LabConfig(required_params={
        'ports': ['zlock'],
        'servers': ['zlock']
    })
    host = config.get('servers', 'zlock')
    port = config.get('ports', 'zlock')
    if socket.gethostbyname(host) == socket.gethostbyname('localhost'):
        try:
            # short connection timeout if localhost, don't want to
            # waste time:
            zprocess.locking.connect(host, port, timeout=0.05)
        except zmq.ZMQError:
            # No zprocess.locking server running on localhost. Start one. It will run
            # forever, even after this program exits. This is important for
            # other programs which might be using it. I don't really consider
            # this bad practice since the server is typically supposed to
            # be running all the time:
            start_daemon([sys.executable, '-m', 'zprocess.locking'])
            # Try again. Longer timeout this time, give it time to start up:
            zprocess.locking.connect(host, port, timeout=15)
    else:
        zprocess.locking.connect(host, port)

    # Check if the zlock server supports read-write locks:
    global _server_supports_readwrite
    if hasattr(zprocess.locking, 'get_protocol_version'):
        version = zprocess.locking.get_protocol_version()
        if LooseVersion(version) >= LooseVersion('1.1.0'):
            _server_supports_readwrite = True

    # The user can call these functions to change the timeouts later if they
    # are not to their liking:
    set_default_timeout(DEFAULT_TIMEOUT)
def connect_to_zlock_server():
    # setup connection with the zprocess.locking server, depending on labconfig settings:
    config = LabConfig(required_params={
        'ports': ['zlock'],
        'servers': ['zlock']
    })
    host = config.get('servers', 'zlock')
    port = config.get('ports', 'zlock')
    if socket.gethostbyname(host) == socket.gethostbyname('localhost'):
        try:
            # short connection timeout if localhost, don't want to
            # waste time:
            zprocess.locking.connect(host, port, timeout=0.05)
        except zmq.ZMQError:
            # No zprocess.locking server running on localhost. Start one. It will run
            # forever, even after this program exits. This is important for
            # other programs which might be using it. I don't really consider
            # this bad practice since the server is typically supposed to
            # be running all the time:
            devnull = open(os.devnull, 'w')
            subprocess.Popen([sys.executable, '-m', 'zprocess.locking'],
                             stdout=devnull,
                             stderr=devnull)
            # Try again. Longer timeout this time, give it time to start up:
            zprocess.locking.connect(host, port, timeout=15)
    else:
        zprocess.locking.connect(host, port)

    # The user can call these functions to change the timeouts later if they
    # are not to their liking:
    set_default_timeout(DEFAULT_TIMEOUT)
def connect_to_zlock_server():
    # setup connection with the zprocess.locking server, depending on labconfig settings:
    config = LabConfig(required_params={
        'ports': ['zlock'],
        'servers': ['zlock']
    })
    host = config.get('servers', 'zlock')
    port = config.get('ports', 'zlock')
    if socket.gethostbyname(host) == socket.gethostbyname('localhost'):
        try:
            # short connection timeout if localhost, don't want to
            # waste time:
            zprocess.locking.connect(host, port, timeout=0.05)
        except zmq.ZMQError:
            # No zprocess.locking server running on localhost. Start one. It will run
            # forever, even after this program exits. This is important for
            # other programs which might be using it. I don't really consider
            # this bad practice since the server is typically supposed to
            # be running all the time:
            if os.name == 'nt':
                creationflags = 0x00000008  # DETACHED_PROCESS from the win32 API
                # Note that we must not remain in same working directory, or we will hold a lock
                # on it that prevents it from being deleted.
                subprocess.Popen([sys.executable, '-m', 'zprocess.locking'],
                                 creationflags=creationflags,
                                 stdout=None,
                                 stderr=None,
                                 close_fds=True,
                                 cwd=os.getenv('temp'))
            else:
                devnull = open(os.devnull, 'w')
                if not os.fork():
                    os.setsid()
                    subprocess.Popen(
                        [sys.executable, '-m', 'zprocess.locking'],
                        stdin=devnull,
                        stdout=devnull,
                        stderr=devnull,
                        close_fds=True)
                    os._exit(0)
            # Try again. Longer timeout this time, give it time to start up:
            zprocess.locking.connect(host, port, timeout=15)
    else:
        zprocess.locking.connect(host, port)

    # The user can call these functions to change the timeouts later if they
    # are not to their liking:
    set_default_timeout(DEFAULT_TIMEOUT)
Esempio n. 4
0
def connect_to_zlock_server():
    # setup connection with the zprocess.locking server, depending on labconfig settings: 
    config = LabConfig(required_params={'ports':['zlock'],'servers':['zlock']})
    host = config.get('servers','zlock')
    port = config.get('ports','zlock')
    if socket.gethostbyname(host) == socket.gethostbyname('localhost'):
        try:
            # short connection timeout if localhost, don't want to
            # waste time:
            zprocess.locking.connect(host,port,timeout=0.05)
        except zmq.ZMQError:
            # No zprocess.locking server running on localhost. Start one. It will run
            # forever, even after this program exits. This is important for
            # other programs which might be using it. I don't really consider
            # this bad practice since the server is typically supposed to
            # be running all the time:
            if os.name == 'nt':
                creationflags=0x00000008 # DETACHED_PROCESS from the win32 API
                # Note that we must not remain in same working directory, or we will hold a lock
                # on it that prevents it from being deleted.
                subprocess.Popen([sys.executable,'-m','zprocess.locking'],
                                 creationflags=creationflags, stdout=None, stderr=None,
                                 close_fds=True, cwd=os.getenv('temp'))
            else:
                devnull = open(os.devnull,'w')
                if not os.fork():
                    os.setsid()
                    subprocess.Popen([sys.executable,'-m','zprocess.locking'],
                                     stdin=devnull, stdout=devnull, stderr=devnull, close_fds=True)
                    os._exit(0)
            # Try again. Longer timeout this time, give it time to start up:
            zprocess.locking.connect(host,port,timeout=15)
    else:
        zprocess.locking.connect(host, port)

    # The user can call these functions to change the timeouts later if they
    # are not to their liking:
    set_default_timeout(DEFAULT_TIMEOUT)