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


Python os.closerange方法代碼示例

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


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

示例1: _close_fds

# 需要導入模塊: import os [as 別名]
# 或者: from os import closerange [as 別名]
def _close_fds(self, but):
            if hasattr(os, 'closerange'):
                os.closerange(3, but)
                os.closerange(but + 1, MAXFD)
            else:
                for i in xrange(3, MAXFD):
                    if i == but:
                        continue
                    try:
                        os.close(i)
                    except:
                        pass


        # Used as a bandaid workaround for https://bugs.python.org/issue27448
        # to prevent multiple simultaneous subprocess launches from interfering
        # with one another and leaving gc disabled. 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:19,代碼來源:subprocess.py

示例2: test_closerange

# 需要導入模塊: import os [as 別名]
# 或者: from os import closerange [as 別名]
def test_closerange(self):
        first = os.open(test_support.TESTFN, os.O_CREAT|os.O_RDWR)
        # We must allocate two consecutive file descriptors, otherwise
        # it will mess up other file descriptors (perhaps even the three
        # standard ones).
        second = os.dup(first)
        try:
            retries = 0
            while second != first + 1:
                os.close(first)
                retries += 1
                if retries > 10:
                    # XXX test skipped
                    self.skipTest("couldn't allocate two consecutive fds")
                first, second = second, os.dup(second)
        finally:
            os.close(second)
        # close a fd that is open, and one that isn't
        os.closerange(first, first + 2)
        self.assertRaises(OSError, os.write, first, "a") 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:22,代碼來源:test_os.py

示例3: test_open

# 需要導入模塊: import os [as 別名]
# 或者: from os import closerange [as 別名]
def test_open(self):
        test_filename = "tmp.open.test"
        fd1 = os.open(test_filename + "1", flags)

        # make sure fd+1 and fd+2 are closed
        os.closerange(fd1 + 1, fd1 + 2)

        # open should return the lowest-numbered file descriptor not currently open
        # for the process
        fd2 = os.open(test_filename + "2", flags)
        fd3 = os.open(test_filename + "3", flags)

        os.close(fd2)
        self.assertRaisesMessage(OSError, "[Errno 9] Bad file descriptor", os.lseek, fd2, os.SEEK_SET, 0)

        fd4 = os.open(test_filename + "4", flags)
        self.assertEqual(fd4, fd2)

        os.close(fd1)
        os.close(fd3)
        os.close(fd4)

        for i in range(1, 5):
            os.unlink(test_filename + str(i)) 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:26,代碼來源:test_fd.py

示例4: test_closerange

# 需要導入模塊: import os [as 別名]
# 或者: from os import closerange [as 別名]
def test_closerange(self):
        first = os.open(support.TESTFN, os.O_CREAT|os.O_RDWR)
        # We must allocate two consecutive file descriptors, otherwise
        # it will mess up other file descriptors (perhaps even the three
        # standard ones).
        second = os.dup(first)
        try:
            retries = 0
            while second != first + 1:
                os.close(first)
                retries += 1
                if retries > 10:
                    # XXX test skipped
                    self.skipTest("couldn't allocate two consecutive fds")
                first, second = second, os.dup(second)
        finally:
            os.close(second)
        # close a fd that is open, and one that isn't
        os.closerange(first, first + 2)
        self.assertRaises(OSError, os.write, first, b"a") 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:22,代碼來源:test_os.py

示例5: _close_fds

# 需要導入模塊: import os [as 別名]
# 或者: from os import closerange [as 別名]
def _close_fds(self, fds_to_keep):
            start_fd = 3
            for fd in sorted(fds_to_keep):
                if fd >= start_fd:
                    os.closerange(start_fd, fd)
                    start_fd = fd + 1
            if start_fd <= MAXFD:
                os.closerange(start_fd, MAXFD) 
開發者ID:war-and-code,項目名稱:jawfish,代碼行數:10,代碼來源:subprocess.py

示例6: closerange

# 需要導入模塊: import os [as 別名]
# 或者: from os import closerange [as 別名]
def closerange(fd_low, fd_high):
        # Iterate through and close all file descriptors.
        for fd in range(fd_low, fd_high):
            try:
                os.close(fd)
            except OSError:  # ERROR, fd wasn't open to begin with (ignored)
                pass 
開發者ID:jpush,項目名稱:jbox,代碼行數:9,代碼來源:util.py

示例7: exit

# 需要導入模塊: import os [as 別名]
# 或者: from os import closerange [as 別名]
def exit():
    """
    Causes python to exit without garbage-collecting any objects, and thus avoids
    calling object destructor methods. This is a sledgehammer workaround for 
    a variety of bugs in PyQt and Pyside that cause crashes on exit.
    
    This function does the following in an attempt to 'safely' terminate
    the process:
    
    * Invoke atexit callbacks
    * Close all open file handles
    * os._exit()
    
    Note: there is some potential for causing damage with this function if you
    are using objects that _require_ their destructors to be called (for example,
    to properly terminate log files, disconnect from devices, etc). Situations
    like this are probably quite rare, but use at your own risk.
    """
    
    ## first disable our own cleanup function; won't be needing it.
    setConfigOptions(exitCleanup=False)
    
    ## invoke atexit callbacks
    atexit._run_exitfuncs()
    
    ## close file handles
    if sys.platform == 'darwin':
        for fd in range(3, 4096):
            if fd not in [7]:  # trying to close 7 produces an illegal instruction on the Mac.
                os.close(fd)
    else:
        os.closerange(3, 4096) ## just guessing on the maximum descriptor count..

    os._exit(0)
    


## Convenience functions for command-line use 
開發者ID:SrikanthVelpuri,項目名稱:tf-pose,代碼行數:40,代碼來源:__init__.py

示例8: _run_child

# 需要導入模塊: import os [as 別名]
# 或者: from os import closerange [as 別名]
def _run_child(self, cmd):
        if isinstance(cmd, basestring):
            cmd = ['/bin/sh', '-c', cmd]
        os.closerange(3, MAXFD)
        try:
            os.execvp(cmd[0], cmd)
        finally:
            os._exit(1) 
開發者ID:glmcdona,項目名稱:meddle,代碼行數:10,代碼來源:popen2.py

示例9: _close_fds

# 需要導入模塊: import os [as 別名]
# 或者: from os import closerange [as 別名]
def _close_fds(self, but):
            if hasattr(os, 'closerange'):
                os.closerange(3, but)
                os.closerange(but + 1, MAXFD)
            else:
                for i in xrange(3, MAXFD):
                    if i == but:
                        continue
                    try:
                        os.close(i)
                    except:
                        pass 
開發者ID:glmcdona,項目名稱:meddle,代碼行數:14,代碼來源:subprocess.py

示例10: _run_as_another_namespace

# 需要導入模塊: import os [as 別名]
# 或者: from os import closerange [as 別名]
def _run_as_another_namespace(
        pid,
        namespaces,
        function,
        _args=(),
        _kwargs={}
):

    # os.closerange(1, 1000)
    context = ProcessContext(pid, namespaces)
    context.attach()
    try:
        return run_as_another_process(function, _args, _kwargs)
    finally:
        context.detach() 
開發者ID:cloudviz,項目名稱:agentless-system-crawler,代碼行數:17,代碼來源:namespace.py

示例11: _close_fds

# 需要導入模塊: import os [as 別名]
# 或者: from os import closerange [as 別名]
def _close_fds(self, keep):
            # `keep` is a set of fds, so we
            # use os.closerange from 3 to min(keep)
            # and then from max(keep + 1) to MAXFD and
            # loop through filling in the gaps.
            # Under new python versions, we need to explicitly set
            # passed fds to be inheritable or they will go away on exec
            if hasattr(os, 'set_inheritable'):
                set_inheritable = os.set_inheritable
            else:
                set_inheritable = lambda i, v: True
            if hasattr(os, 'closerange'):
                keep = sorted(keep)
                min_keep = min(keep)
                max_keep = max(keep)
                os.closerange(3, min_keep)
                os.closerange(max_keep + 1, MAXFD)
                for i in xrange(min_keep, max_keep):
                    if i in keep:
                        set_inheritable(i, True)
                        continue

                    try:
                        os.close(i)
                    except:
                        pass
            else:
                for i in xrange(3, MAXFD):
                    if i in keep:
                        set_inheritable(i, True)
                        continue
                    try:
                        os.close(i)
                    except:
                        pass 
開發者ID:leancloud,項目名稱:satori,代碼行數:37,代碼來源:subprocess.py


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