當前位置: 首頁>>代碼示例>>Python>>正文


Python locks.LOCK_EX屬性代碼示例

本文整理匯總了Python中django.core.files.locks.LOCK_EX屬性的典型用法代碼示例。如果您正苦於以下問題:Python locks.LOCK_EX屬性的具體用法?Python locks.LOCK_EX怎麽用?Python locks.LOCK_EX使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在django.core.files.locks的用法示例。


在下文中一共展示了locks.LOCK_EX屬性的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: load_file

# 需要導入模塊: from django.core.files import locks [as 別名]
# 或者: from django.core.files.locks import LOCK_EX [as 別名]
def load_file(cls, file_name):
        try:
            with open(file_name) as fp:
                locks.lock(fp, locks.LOCK_EX)
                content = fp.read()
        except OSError as exc:
            if exc.errno == errno.ENOENT:
                content = "{}"
            else:
                raise

        data = yaml.safe_load(content)

        if data is None:
            return {}
        elif not isinstance(data, dict):
            raise TypeError("YAML content of {} is not a dictionary".format(file_name))

        return data 
開發者ID:adamchainz,項目名稱:django-perf-rec,代碼行數:21,代碼來源:yaml.py

示例2: set_and_save

# 需要導入模塊: from django.core.files import locks [as 別名]
# 或者: from django.core.files.locks import LOCK_EX [as 別名]
def set_and_save(self, key, value):
        if self.data.get(key, object()) == value:
            return

        fd = os.open(self.file_name, os.O_RDWR | os.O_CREAT, mode=0o666)
        with os.fdopen(fd, "r+") as fp:
            locks.lock(fd, locks.LOCK_EX)

            data = yaml.safe_load(fp)
            if data is None:
                data = {}

            self.data[key] = value
            data[key] = value

            fp.seek(0)
            yaml.safe_dump(
                data, fp, default_flow_style=False, allow_unicode=True, width=10000
            ) 
開發者ID:adamchainz,項目名稱:django-perf-rec,代碼行數:21,代碼來源:yaml.py

示例3: setUpClass

# 需要導入模塊: from django.core.files import locks [as 別名]
# 或者: from django.core.files.locks import LOCK_EX [as 別名]
def setUpClass(cls):
        if cls.lockfile is None:
            raise ValueError(
                "{}.lockfile isn't set. Set it to a unique value "
                "in the base class.".format(cls.__name__))
        cls._lockfile = open(cls.lockfile)
        locks.lock(cls._lockfile, locks.LOCK_EX)
        super().setUpClass() 
開發者ID:reBiocoder,項目名稱:bioforum,代碼行數:10,代碼來源:testcases.py

示例4: setUpClass

# 需要導入模塊: from django.core.files import locks [as 別名]
# 或者: from django.core.files.locks import LOCK_EX [as 別名]
def setUpClass(cls):
        if cls.lockfile is None:
            raise ValueError(
                "{}.lockfile isn't set. Set it to a unique value "
                "in the base class.".format(cls.__name__))
        cls._lockfile = open(cls.lockfile)
        locks.lock(cls._lockfile, locks.LOCK_EX)
        super(SerializeMixin, cls).setUpClass() 
開發者ID:Yeah-Kun,項目名稱:python,代碼行數:10,代碼來源:testcases.py

示例5: file_move_safe

# 需要導入模塊: from django.core.files import locks [as 別名]
# 或者: from django.core.files.locks import LOCK_EX [as 別名]
def file_move_safe(old_file_name, new_file_name, chunk_size=1024 * 64, allow_overwrite=False):
    """
    Moves a file from one location to another in the safest way possible.

    First, tries ``os.rename``, which is simple but will break across filesystems.
    If that fails, streams manually from one file to another in pure Python.

    If the destination file exists and ``allow_overwrite`` is ``False``, this
    function will throw an ``IOError``.
    """

    # There's no reason to move if we don't have to.
    if _samefile(old_file_name, new_file_name):
        return

    try:
        # If the destination file exists and allow_overwrite is False then raise an IOError
        if not allow_overwrite and os.access(new_file_name, os.F_OK):
            raise IOError("Destination file %s exists and allow_overwrite is False" % new_file_name)

        os.rename(old_file_name, new_file_name)
        return
    except OSError:
        # This will happen with os.rename if moving to another filesystem
        # or when moving opened files on certain operating systems
        pass

    # first open the old file, so that it won't go away
    with open(old_file_name, 'rb') as old_file:
        # now open the new file, not forgetting allow_overwrite
        fd = os.open(new_file_name, (os.O_WRONLY | os.O_CREAT | getattr(os, 'O_BINARY', 0) |
                                     (os.O_EXCL if not allow_overwrite else 0)))
        try:
            locks.lock(fd, locks.LOCK_EX)
            current_chunk = None
            while current_chunk != b'':
                current_chunk = old_file.read(chunk_size)
                os.write(fd, current_chunk)
        finally:
            locks.unlock(fd)
            os.close(fd)
    copystat(old_file_name, new_file_name)

    try:
        os.remove(old_file_name)
    except OSError as e:
        # Certain operating systems (Cygwin and Windows)
        # fail when deleting opened files, ignore it.  (For the
        # systems where this happens, temporary files will be auto-deleted
        # on close anyway.)
        if getattr(e, 'winerror', 0) != 32 and getattr(e, 'errno', 0) != 13:
            raise 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:54,代碼來源:move.py

示例6: file_move_safe

# 需要導入模塊: from django.core.files import locks [as 別名]
# 或者: from django.core.files.locks import LOCK_EX [as 別名]
def file_move_safe(old_file_name, new_file_name, chunk_size = 1024*64, allow_overwrite=False):
    """
    Moves a file from one location to another in the safest way possible.

    First, tries ``os.rename``, which is simple but will break across filesystems.
    If that fails, streams manually from one file to another in pure Python.

    If the destination file exists and ``allow_overwrite`` is ``False``, this
    function will throw an ``IOError``.
    """

    # There's no reason to move if we don't have to.
    if _samefile(old_file_name, new_file_name):
        return

    try:
        os.rename(old_file_name, new_file_name)
        return
    except OSError:
        # This will happen with os.rename if moving to another filesystem
        # or when moving opened files on certain operating systems
        pass

    # first open the old file, so that it won't go away
    with open(old_file_name, 'rb') as old_file:
        # now open the new file, not forgetting allow_overwrite
        fd = os.open(new_file_name, os.O_WRONLY | os.O_CREAT | getattr(os, 'O_BINARY', 0) |
                                    (not allow_overwrite and os.O_EXCL or 0))
        try:
            locks.lock(fd, locks.LOCK_EX)
            current_chunk = None
            while current_chunk != b'':
                current_chunk = old_file.read(chunk_size)
                os.write(fd, current_chunk)
        finally:
            locks.unlock(fd)
            os.close(fd)
    copystat(old_file_name, new_file_name)

    try:
        os.remove(old_file_name)
    except OSError as e:
        # Certain operating systems (Cygwin and Windows) 
        # fail when deleting opened files, ignore it.  (For the 
        # systems where this happens, temporary files will be auto-deleted
        # on close anyway.)
        if getattr(e, 'winerror', 0) != 32 and getattr(e, 'errno', 0) != 13:
            raise 
開發者ID:blackye,項目名稱:luscan-devel,代碼行數:50,代碼來源:move.py

示例7: _save

# 需要導入模塊: from django.core.files import locks [as 別名]
# 或者: from django.core.files.locks import LOCK_EX [as 別名]
def _save(self, name, content):
        full_path = self.path(name)

        directory = os.path.dirname(full_path)
        if not os.path.exists(directory):
            os.makedirs(directory)
        elif not os.path.isdir(directory):
            raise IOError("%s exists and is not a directory." % directory)

        # There's a potential race condition between get_available_name and
        # saving the file; it's possible that two threads might return the
        # same name, at which point all sorts of fun happens. So we need to
        # try to create the file, but if it already exists we have to go back
        # to get_available_name() and try again.

        while True:
            try:
                # This file has a file path that we can move.
                if hasattr(content, 'temporary_file_path'):
                    file_move_safe(content.temporary_file_path(), full_path)
                    content.close()

                # This is a normal uploadedfile that we can stream.
                else:
                    # This fun binary flag incantation makes os.open throw an
                    # OSError if the file already exists before we open it.
                    fd = os.open(full_path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, 'O_BINARY', 0))
                    try:
                        locks.lock(fd, locks.LOCK_EX)
                        for chunk in content.chunks():
                            os.write(fd, chunk)
                            # CHANGED: This un-hangs us long enough to keep things rolling.
                            eventlet.sleep(0)
                    finally:
                        locks.unlock(fd)
                        os.close(fd)
            except OSError, e:
                if e.errno == errno.EEXIST:
                    # Ooops, the file exists. We need a new file name.
                    name = self.get_available_name(name)
                    full_path = self.path(name)
                else:
                    raise
            else:
                # OK, the file save worked. Break out of the loop.
                break 
開發者ID:astrobin,項目名稱:astrobin,代碼行數:48,代碼來源:standard_gunicorn_eventlet.py


注:本文中的django.core.files.locks.LOCK_EX屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。