test_threadedtempfile.py

来自「mallet是自然语言处理、机器学习领域的一个开源项目。」· Python 代码 · 共 87 行

PY
87
字号
"""Create and delete FILES_PER_THREAD temp files (via tempfile.TemporaryFile)in each of NUM_THREADS threads, recording the number of successes andfailures.  A failure is a bug in tempfile, and may be due to:+ Trying to create more than one tempfile with the same name.+ Trying to delete a tempfile that doesn't still exist.+ Something we've never seen before.By default, NUM_THREADS == 20 and FILES_PER_THREAD == 50.  This is enough tocreate about 150 failures per run under Win98SE in 2.0, and runs prettyquickly. Guido reports needing to boost FILES_PER_THREAD to 500 beforeprovoking a 2.0 failure under Linux.  Run the test alone to boost eithervia cmdline switches:-f  FILES_PER_THREAD (int)-t  NUM_THREADS (int)"""NUM_THREADS = 20        # change w/ -t optionFILES_PER_THREAD = 50   # change w/ -f optionimport thread # If this fails, we can't test this moduleimport threadingfrom test_support import TestFailedimport StringIOfrom traceback import print_excstartEvent = threading.Event()import tempfiletempfile.gettempdir() # Do this now, to avoid spurious races laterclass TempFileGreedy(threading.Thread):    error_count = 0    ok_count = 0    def run(self):        self.errors = StringIO.StringIO()        startEvent.wait()        for i in range(FILES_PER_THREAD):            try:                f = tempfile.TemporaryFile("w+b")                f.close()            except:                self.error_count += 1                print_exc(file=self.errors)            else:                self.ok_count += 1def _test():    threads = []    print "Creating"    for i in range(NUM_THREADS):        t = TempFileGreedy()        threads.append(t)        t.start()    print "Starting"    startEvent.set()    print "Reaping"    ok = errors = 0    for t in threads:        t.join()        ok += t.ok_count        errors += t.error_count        if t.error_count:            print '%s errors:\n%s' % (t.getName(), t.errors.getvalue())    msg = "Done: errors %d ok %d" % (errors, ok)    print msg    if errors:        raise TestFailed(msg)if __name__ == "__main__":    import sys, getopt    opts, args = getopt.getopt(sys.argv[1:], "t:f:")    for o, v in opts:        if o == "-f":            FILES_PER_THREAD = int(v)        elif o == "-t":            NUM_THREADS = int(v)_test()

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?