python类LOCK_UN的实例源码

queue.py 文件源码 项目:temboard-agent 作者: dalibo 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def get_content_all_messages(self):
        """
        Get all messages
        """
        try:
            buffers = list()
            with open(self.file_path, 'r') as fd:
                fcntl.flock(fd, fcntl.LOCK_SH)
                for row_msg in fd.readlines():
                    try:
                        msg = self.parse_row_message(row_msg)
                        buffers.append(json.loads(msg.content))
                    except Exception:
                        pass
                fcntl.flock(fd, fcntl.LOCK_UN)
            return buffers
        except Exception:
            return
queue.py 文件源码 项目:temboard-agent 作者: dalibo 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def push(self, message):
        """ Push a new message. """
        if self.overflow_mode == 'drop':
            if self.max_length > -1 and self.get_length() >= self.max_length:
                return
            if self.max_size > -1 and self.get_size() >= self.max_size:
                return

        with open(self.file_path, 'a') as fd:
            # Let's hold an exclusive lock.
            fcntl.flock(fd, fcntl.LOCK_EX)
            fd.write(message.serialize())
            fcntl.flock(fd, fcntl.LOCK_UN)
            fd.close()

        if self.overflow_mode == 'slide':
            if self.max_size == -1 and self.max_length > -1:
                while self.get_length() > self.max_length:
                    self.shift()
            elif self.max_size > -1 and self.max_length == -1:
                while self.get_size() > self.max_size:
                    self.shift()
lock.py 文件源码 项目:landscape-client 作者: CanonicalLtd 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def lock_path(path, timeout=0):
    fd = os.open(path, os.O_CREAT)
    flags = fcntl.fcntl(fd, fcntl.F_GETFD, 0)
    flags |= fcntl.FD_CLOEXEC
    fcntl.fcntl(fd, fcntl.F_SETFD, flags)

    started = time.time()

    while True:
        try:
            fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
        except IOError:
            if started < time.time() - timeout:
                raise LockError("Couldn't obtain lock")
        else:
            break
        time.sleep(0.1)

    def unlock_path():
        fcntl.flock(fd, fcntl.LOCK_UN)
        os.close(fd)

    return unlock_path
minc_tools.py 文件源码 项目:nist_mni_pipelines 作者: vfonov 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def unlock(self,fname):
        #TODO: something more clever here?
        lock_name=fname+'.lock'
        try:
            f=self._locks[lock_name]

            if f is not None:
                fcntl.lockf(f.fileno(), fcntl.LOCK_UN)
                f.close()

            del self._locks[lock_name]

#            try:
#                os.unlink(lock_name)
#            except OSError:
                #probably somebody else is blocking
#               pass

        except KeyError:
            pass


    #def __del__(self):
        #self.do_cleanup()
    #    pass
lock.py 文件源码 项目:karton 作者: karton 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def release(self):
        '''
        Release a previously acquired lock.
        '''
        assert self._locked
        assert self._lock_file

        # Note that this actually leaves the lock file around, but deleting it without a race
        # is not trivial.
        fcntl.flock(self._lock_file, fcntl.LOCK_UN)
        verbose('Lock "%s" released.' % self._lock_file_path)

        self._locked = False

        self._lock_file.close()
        self._lock_file = None
Quads.py 文件源码 项目:quads 作者: redhat-performance 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def write_data(self):
        if self.config_newer_than_data():
            self.read_data()
            return False
        else:
            try:
                self.data = {"clouds":self.quads.clouds.data, "hosts":self.quads.hosts.data, "history":self.quads.history.data, "cloud_history":self.quads.cloud_history.data}
                with open(self.config, 'w') as yaml_file:
                    fcntl.flock(yaml_file, fcntl.LOCK_EX | fcntl.LOCK_NB)
                    yaml_file.write(yaml.dump(self.data, default_flow_style=False))
                    fcntl.flock(yaml_file, fcntl.LOCK_UN)
                self.read_data()
                return True
            except Exception, ex:
                self.logger.error("There was a problem with your file %s" % ex)
                return False
load.py 文件源码 项目:raptiformica 作者: vdloo 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def config_cache_lock():
    """
    Obtain the config cache lock, perform the code
    in the context and then let the lock go.
    :yield None
    :return None:
    """
    with open(conf().CONFIG_CACHE_LOCK, 'w+') as lock:
        try:
            log.debug(
                "Getting config cache lock. "
                "If this blocks forever, try deleting file "
                "{} and restart the process.".format(conf().CONFIG_CACHE_LOCK)
            )
            flock(lock, LOCK_EX)  # Blocks until lock becomes available
            yield
        finally:
            log.debug("Releasing the config cache lock")
            flock(lock, LOCK_UN)
tendo_singleton.py 文件源码 项目:QTodoTxt2 作者: QTodoTxt 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def __del__(self):
        import sys
        import os
        if not self.initialized:
            return
        try:
            if sys.platform == 'win32':
                if hasattr(self, 'fd'):
                    os.close(self.fd)
                    os.unlink(self.lockfile)
            else:
                import fcntl
                fcntl.lockf(self.fp, fcntl.LOCK_UN)
                # os.close(self.fp)
                if os.path.isfile(self.lockfile):
                    os.unlink(self.lockfile)
        except Exception as e:
            raise
prometheus.py 文件源码 项目:dramatiq 作者: Bogdanp 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def flock(path):
    """Attempt to acquire a POSIX file lock.
    """
    with open(path, "w+") as lf:
        try:
            fcntl.flock(lf, fcntl.LOCK_EX | fcntl.LOCK_NB)
            acquired = True
            yield acquired

        except OSError:
            acquired = False
            yield acquired

        finally:
            if acquired:
                fcntl.flock(lf, fcntl.LOCK_UN)
helpers.py 文件源码 项目:buckit 作者: facebookexperimental 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def open_with_lock(filename, mode):
    with open(filename, mode) as f:
        while True:
            try:
                fcntl.flock(f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
                yield f
                break
            except IOError as e:
                # raise on unrelated IOErrors
                if e.errno != errno.EAGAIN:
                    raise
                else:
                    time.sleep(0.1)
    try:
        fcntl.flock(f.fileno(), fcntl.LOCK_UN)
    except Exception:
        pass
lockfile.py 文件源码 项目:solaris-ips 作者: oracle 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def unlock(self):
                """Unlocks the LockFile."""

                if self._fileobj:
                        # To avoid race conditions with the next caller
                        # waiting for the lock file, it is simply
                        # truncated instead of removed.
                        try:
                                fcntl.lockf(self._fileobj, fcntl.LOCK_UN)
                                self._fileobj.truncate(0)
                                self._fileobj.close()
                                self._lock.release()
                        except EnvironmentError:
                                # If fcntl, or the file operations returned
                                # an exception, drop the lock. Do not catch
                                # the exception that could escape from
                                # releasing the lock.
                                self._lock.release()
                                raise
                        finally:
                                self._fileobj = None
                else:
                        if self._provide_mutex:
                                assert not self._lock.locked
db.py 文件源码 项目:zabbix-alert 作者: annProg 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def updateDB(file_db, msg):
    db = init_DB(file_db)
    if not db:
        data = msg
    else:
        namelist = db['??'].split(",")
        if msg['??'] not in namelist:
            namelist.append(msg['??'])
        db['??'] = ",".join(namelist)
        try:
            db['eventid'] = db['eventid'] + "," + msg['eventid']
        except:
            pass
#       if db['??'] != msg['??']:
#           db['??'] = db['??'] + "," + msg['??']
        if "IP" in msg and "IP" in db:
            if db['IP'] != msg['IP']:
                db['IP'] = db['IP'] + "," + msg['IP']
        db['??'] = db['??'] + msg['??']
        data = db

    with open(file_db, 'w') as f:
        fcntl.flock(f, fcntl.LOCK_EX)
        json.dump(data, f, ensure_ascii=False)
        fcntl.flock(f, fcntl.LOCK_UN)
MS_ILI9341.py 文件源码 项目:PiStorms 作者: mindsensors 项目源码 文件源码 阅读 47 收藏 0 点赞 0 评论 0
def display(self, image=None):
        flock(self.mutex, LOCK_EX)
        content = self.readRecordingCount()
        if len(content) == 2 and self.isTakingFrames(content[0]):
            self.decrementRecordingCount(content[0],self.isStoringWithBg(content[1]))
            if self.isStoringWithBg(content[1]): self.save(includeBg=True)
            else: self.save(includeBg=False)
        if image is None: image = self.buffer
        self.set_window()
        pixelbytes = list(Adafruit_ILI9341.image_to_data(image))
        self.data(pixelbytes)
        if self.store and self.isTakingFrames(self.readTouchRecordingCount()[0]):
            image = Image.new('RGBA',(568, 428))
            draw = ImageDraw.Draw(image)
            draw.ellipse((122+320-self.y, 12+self.x, 152+320-self.y, 42+self.x), fill = 'red', outline ='red')
            self.save(img=image)
            self.store = False
            self.x, self.y = -1, -1
        flock(self.mutex, LOCK_UN)
metadata.py 文件源码 项目:rca-evaluation 作者: sieve-microservices 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def update(path):
    """
    allow concurrent update of metadata
    """
    p = os.path.join(path, "metadata.json")
    # we have to open writeable to get a lock
    with open(p, "a") as f:
        fcntl.lockf(f, fcntl.LOCK_EX)
        data = load(path)
        yield(data)
        save(path, data)
        fcntl.lockf(f, fcntl.LOCK_UN)
mailbox.py 文件源码 项目:kinect-2-libras 作者: inessadl 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def _unlock_file(f):
    """Unlock file f using lockf and dot locking."""
    if fcntl:
        fcntl.lockf(f, fcntl.LOCK_UN)
    if os.path.exists(f.name + '.lock'):
        os.remove(f.name + '.lock')
locked_file.py 文件源码 项目:oscars2016 作者: 0x0ece 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def unlock_and_close(self):
            """Close and unlock the file using the fcntl.lockf primitive."""
            if self._locked:
                fcntl.lockf(self._fh.fileno(), fcntl.LOCK_UN)
            self._locked = False
            if self._fh:
                self._fh.close()
ceph_rgw.py 文件源码 项目:DeepSea 作者: SUSE 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def main():
    exit_status = 1
    try:
        args = parse_args()
        # Make sure the exporter is only running once.
        lock_file = '/var/lock/{}.lock'.format(os.path.basename(sys.argv[0]))
        lock_fd = os.open(lock_file, os.O_CREAT)
        lock_success = False
        try:
            fcntl.flock(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
            lock_success = True
        except IOError:
            msg = 'Failed to export metrics, another instance is running.'
            syslog.syslog(syslog.LOG_INFO, msg)
            sys.stderr.write(msg + '\n')
        if lock_success:
            # Create a new registry, otherwise unwanted default collectors are
            # added automatically.
            registry = prometheus_client.CollectorRegistry()
            # Register our own collector and write metrics to STDOUT.
            registry.register(CephRgwCollector(**vars(args)))
            sys.stdout.write(prometheus_client.generate_latest(registry))
            sys.stdout.flush()
            # Unlock the lock file.
            fcntl.flock(lock_fd, fcntl.LOCK_UN)
            exit_status = 0
    except Exception as e:
        syslog.syslog(syslog.LOG_ERR, str(e))
    # Cleanup
    os.close(lock_fd)
    if lock_success:
        try:
            os.unlink(lock_file)
        except:
            pass
    sys.exit(exit_status)
recipe-578476.py 文件源码 项目:code 作者: ActiveState 项目源码 文件源码 阅读 36 收藏 0 点赞 0 评论 0
def clean_up(self):
        # this is not really needed
        try:
            if self.fh is not None:
                if OS_WIN:
                    os.close(self.fh)
                    os.unlink(LOCK_PATH)
                else:
                    fcntl.lockf(self.fh, fcntl.LOCK_UN)
                    self.fh.close() # ???
                    os.unlink(LOCK_PATH)
        except Exception as err:
            # logger.exception(err)
            raise # for debugging porpuses, do not raise it on production
recipe-577404.py 文件源码 项目:code 作者: ActiveState 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def lockfile(file):
    "flock a given file, then unflock it immediately"
    if _lockonly(file):
        flock(file, LOCK_UN)

# Options
recipe-578998.py 文件源码 项目:code 作者: ActiveState 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def __init__(self, mutex_name):
            check_valid_mutex_name(mutex_name)
            filename = os.path.join(tempfile.gettempdir(), mutex_name)
            try:
                handle = open(filename, 'w')
                fcntl.flock(handle, fcntl.LOCK_EX | fcntl.LOCK_NB)
            except:
                self._release_mutex = NULL
                self._acquired = False
                try:
                    handle.close()
                except:
                    pass
            else:
                def release_mutex(*args, **kwargs):
                    # Note: can't use self here!
                    if not getattr(release_mutex, 'called', False):
                        release_mutex.called = True
                        try:
                            fcntl.flock(handle, fcntl.LOCK_UN)
                        except:
                            traceback.print_exc()
                        try:
                            handle.close()
                        except:
                            traceback.print_exc()
                        try:
                            # Removing is pretty much optional (but let's do it to keep the
                            # filesystem cleaner).
                            os.unlink(filename)
                        except:
                            pass

                # Don't use __del__: this approach doesn't have as many pitfalls.
                self._ref = weakref.ref(self, release_mutex)

                self._release_mutex = release_mutex
                self._acquired = True
locked_file.py 文件源码 项目:sndlatr 作者: Schibum 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def unlock_and_close(self):
      """Close and unlock the file using the fcntl.lockf primitive."""
      if self._locked:
        fcntl.lockf(self._fh.fileno(), fcntl.LOCK_UN)
      self._locked = False
      if self._fh:
        self._fh.close()
cache.py 文件源码 项目:jenkins-epo 作者: peopledoc 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def close(self):
        self.save()
        self.storage.close()
        if self.lock:
            fcntl.lockf(self.lock, fcntl.LOCK_UN)
            self.lock.close()
            os.unlink(self.lock.name)
        self.opened = False
daemonize.py 文件源码 项目:pykit 作者: baishancloud 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def unlock(self):

        if self.lockfp is None:
            return

        fd = self.lockfp.fileno()
        fcntl.lockf(fd, fcntl.LOCK_UN)
        self.lockfp.close()
        self.lockfp = None
services.py 文件源码 项目:abusehelper 作者: Exploit-install 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def unlock_file(fileobj):
    fcntl.flock(fileobj, fcntl.LOCK_UN)
maildirbot.py 文件源码 项目:abusehelper 作者: Exploit-install 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def lockfile(filename):
    with open(filename, "wb") as opened:
        fd = opened.fileno()
        try:
            fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
        except IOError as ioe:
            if ioe.errno not in (errno.EACCES, errno.EAGAIN):
                raise
            yield False
        else:
            try:
                yield True
            finally:
                fcntl.flock(fd, fcntl.LOCK_UN)
locks.py 文件源码 项目:CodingDojo 作者: ComputerSocietyUNB 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def unlock(f):
            ret = fcntl.flock(_fd(f), fcntl.LOCK_UN)
            return (ret == 0)
locks.py 文件源码 项目:NarshaTech 作者: KimJangHyeon 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def unlock(f):
            ret = fcntl.flock(_fd(f), fcntl.LOCK_UN)
            return (ret == 0)
mailbox.py 文件源码 项目:hostapd-mana 作者: adde88 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def _unlock_file(f):
    """Unlock file f using lockf and dot locking."""
    if fcntl:
        fcntl.lockf(f, fcntl.LOCK_UN)
    if os.path.exists(f.name + '.lock'):
        os.remove(f.name + '.lock')
process_lock.py 文件源码 项目:GAMADV-XTD 作者: taers232c 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def unlock(self):
        fcntl.lockf(self.lockfile, fcntl.LOCK_UN)
test.py 文件源码 项目:contrail-ansible-internal 作者: Juniper 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def __exit__(self, exc_type, exc_value, traceback):
        if self.fh is not None:
            fcntl.lockf(self.fh, fcntl.LOCK_UN)
            self.fh.close()
            os.unlink(LOCK_PATH)


问题


面经


文章

微信
公众号

扫码关注公众号