Skip to content

Instantly share code, notes, and snippets.

@dwoz
Last active September 22, 2018 06:54
Show Gist options
  • Save dwoz/e6f85f5131113f20c34f952311292e5d to your computer and use it in GitHub Desktop.
Save dwoz/e6f85f5131113f20c34f952311292e5d to your computer and use it in GitHub Desktop.
Windows max files
#!python
from __future__ import print_function
import os, io
import ctypes, logging
import win32file, win32con
import sys
logging.basicConfig()
log = logging.getLogger()
print("Max files before set {}".format(ctypes.windll.msvcrt._getmaxstdio()))
if sys.version_info.major == 2:
if ctypes.windll.msvcr90._setmaxstdio(2048) == -1:
print("Set max open files failed")
print("Max files after set {}".format(ctypes.windll.msvcr90._getmaxstdio()))
TRY_MAX = 1000000
def openfiles_builtin_open(max=TRY_MAX):
print("Try builtin open: {}".format(max))
files = {}
try:
for a in range(max):
name = 'file_{}'.format(a)
#files[name] = os.open(name, os.O_CREAT, 0x666)
files[name] = open(name, 'w')
except Exception as exc:
print("Got exception {} at {}".format(repr(exc), a))
finally:
for name in files:
files[name].close()
try:
os.remove(name)
except:
pass
def openfiles_createfile(max=TRY_MAX):
print("Try CreateFile open: {}".format(max))
files = {}
try:
for a in range(max):
name = 'file_{}'.format(a)
files[name] = win32file.CreateFile(name,
win32file.GENERIC_WRITE,
0,
None,
win32con.CREATE_NEW,
0,
None)
except Exception as exc:
print("Got exception {} at {}".format(repr(exc), a))
finally:
for name in files:
files[name].Close()
try:
os.remove(name)
except:
pass
def openfiles_msvcrt__open(max=TRY_MAX):
print("Try msvcrt._open open: {}".format(max))
files = {}
try:
for a in range(max):
name = 'file_{}'.format(a)
except Exception as exc:
print("Got exception {} at {}".format(repr(exc), a))
finally:
for name in files:
ctypes.windll.msvcrt.fclose(files[name])
try:
os.remove(name)
except:
pass
def openfiles_c(max=204800):
try:
for a in range(max):
name = 'file_{}'.format(a)
#files[name] = io.open(name, 'w')
files[name] = ctypes.windll.msvcrt._open(name)
print("opened {}".format(max))
finally:
for name in files:
ctypes.windll.msvcrt.fclose(files[name])
try:
os.remove(name)
except:
pass
openfiles_builtin_open()
openfiles_msvcrt__open()
openfiles_createfile()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment