Why python has limit for count of file handles?

前端 未结 6 617
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 05:38

I writed simple code for test, how much files may be open in python script:

for i in xrange(2000):
    fp = open(\'files/file_%d\' % i, \'w\')
    fp.write(s         


        
6条回答
  •  盖世英雄少女心
    2020-11-27 06:22

    To check change the limit of open file handles on Linux, you can use the Python module resource:

    import resource
    
    # the soft limit imposed by the current configuration
    # the hard limit imposed by the operating system.
    soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
    print 'Soft limit is ', soft 
    
    # For the following line to run, you need to execute the Python script as root.
    resource.setrlimit(resource.RLIMIT_NOFILE, (3000, hard))
    

    On Windows, I do as Punit S suggested:

    import platform
    
    if platform.system() == 'Windows':
        import win32file
        win32file._setmaxstdio(2048)
    

提交回复
热议问题