fixed ramlimit4

This commit is contained in:
themixray 2022-01-07 16:08:53 +03:00
parent 97b97310d0
commit 72c3119a71
2 changed files with 43 additions and 19 deletions

View file

@ -12,6 +12,9 @@ try:
nonwin32api = False nonwin32api = False
except: except:
nonwin32api = True nonwin32api = True
import sys as _sys
import warnings as _warn
import winwerror as _we
import requests as _req import requests as _req
import tempfile as _tf import tempfile as _tf
import threading as _t import threading as _t
@ -179,25 +182,46 @@ def create(title=None, size=(0,0), icon=None, resizable=False, noframe=False):
def ramLimit(memory_limit): def ramLimit(memory_limit):
if not nonwin32api: if not nonwin32api:
job_name = '' g_hjob = None
breakaway = 'silent' def create_job(job_name='', breakaway='silent'):
hjob = _w32j.CreateJobObject(None,job_name) hjob = _w32j.CreateJobObject(None, job_name)
if breakaway: if breakaway:
info = _w32j.QueryInformationJobObject(hjob,_w32j.JobObjectExtendedLimitInformation) info = _w32j.QueryInformationJobObject(hjob,
if breakaway=='silent':info['BasicLimitInformation']['LimitFlags']|=(_w32j.JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK) _w32j.JobObjectExtendedLimitInformation)
else:info['BasicLimitInformation']['LimitFlags']|=(_w32j.JOB_OBJECT_LIMIT_BREAKAWAY_OK) if breakaway == 'silent':
_w32j.SetInformationJobObject(hjob,_w32j.JobObjectExtendedLimitInformation,info) info['BasicLimitInformation']['LimitFlags'] |= (
hprocess = _w32a.GetCurrentProcess() _w32j.JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK)
try:_w32j.AssignProcessToJobObject(hjob, hprocess);g_hjob=hjob else:
except _w32j.error as e: info['BasicLimitInformation']['LimitFlags'] |= (
if e.winerror!=winerror.ERROR_ACCESS_DENIED:raise _w32j.JOB_OBJECT_LIMIT_BREAKAWAY_OK)
elif sys.getwindowsversion()>=(6,2):raise _w32j.SetInformationJobObject(hjob,
elif _w32j.IsProcessInJob(hprocess,None):raise _w32j.JobObjectExtendedLimitInformation, info)
warnings.warn('The process is already in a job. Nested jobs are not supported prior to Windows 8.') return hjob
info=_w32j.QueryInformationJobObject(g_hjob,_w32j.JobObjectExtendedLimitInformation) def assign_job(hjob):
info['ProcessMemoryLimit']=memory_limit global g_hjob
info['BasicLimitInformation']['LimitFlags']|=(_w32j.JOB_OBJECT_LIMIT_PROCESS_MEMORY) hprocess = _w32a.GetCurrentProcess()
_w32j.SetInformationJobObject(g_hjob,_w32j.JobObjectExtendedLimitInformation,info) try:
_w32j.AssignProcessToJobObject(hjob, hprocess)
g_hjob = hjob
except _w32j.error as e:
if (e._we != _we.ERROR_ACCESS_DENIED or
_sys.getwindowsversion() >= (6, 2) or
not _w32j.IsProcessInJob(hprocess, None)):
raise
_warn.warn('The process is already in a job. Nested jobs are not '
'supported prior to Windows 8.')
def limit_memory(memory_limit):
if g_hjob is None:
return
info = _w32j.QueryInformationJobObject(g_hjob,
_w32j.JobObjectExtendedLimitInformation)
info['ProcessMemoryLimit'] = memory_limit
info['BasicLimitInformation']['LimitFlags'] |= (
_w32j.JOB_OBJECT_LIMIT_PROCESS_MEMORY)
_w32j.SetInformationJobObject(g_hjob,
_w32j.JobObjectExtendedLimitInformation, info)
assign_job(create_job())
limit_memory(memory_limit)
def close(): def close():
_pg.quit() _pg.quit()

Binary file not shown.