Spaces:
Sleeping
Sleeping
Download src/adapters/sandbox/process_lifecycle.py from dellweb3/VibeCoding: direct link, hf CLI and curl.
- Browser
- Download file 2.84 kB
-
https://huggingface.co/spaces/dellweb3/VibeCoding/resolve/main/src/adapters/sandbox/process_lifecycle.py
- Command line
-
hf download hf://spaces/dellweb3/VibeCoding/src/adapters/sandbox/process_lifecycle.py
-
curl -L -o process_lifecycle.py https://huggingface.co/spaces/dellweb3/VibeCoding/resolve/main/src/adapters/sandbox/process_lifecycle.py
2.84 kB
| """Own only this server's child processes; Windows closes the job on parent exit.""" | |
| import os | |
| import subprocess | |
| def spawn_owned(argv, **kwargs): | |
| process = subprocess.Popen(argv, **kwargs) | |
| if os.name != 'nt': | |
| return process | |
| try: | |
| import ctypes | |
| from ctypes import wintypes | |
| class BasicLimits(ctypes.Structure): | |
| _fields_ = [('process_time', ctypes.c_int64), ('job_time', ctypes.c_int64), | |
| ('flags', wintypes.DWORD), ('min_working_set', ctypes.c_size_t), | |
| ('max_working_set', ctypes.c_size_t), ('active_processes', wintypes.DWORD), | |
| ('affinity', ctypes.c_size_t), ('priority', wintypes.DWORD), | |
| ('scheduling', wintypes.DWORD)] | |
| class IoCounters(ctypes.Structure): | |
| _fields_ = [(name, ctypes.c_uint64) for name in | |
| ('read_ops', 'write_ops', 'other_ops', 'read_bytes', 'write_bytes', 'other_bytes')] | |
| class ExtendedLimits(ctypes.Structure): | |
| _fields_ = [('basic', BasicLimits), ('io', IoCounters), | |
| ('process_memory', ctypes.c_size_t), ('job_memory', ctypes.c_size_t), | |
| ('peak_process_memory', ctypes.c_size_t), ('peak_job_memory', ctypes.c_size_t)] | |
| kernel = ctypes.WinDLL('kernel32', use_last_error=True) | |
| kernel.CreateJobObjectW.argtypes = [ctypes.c_void_p, wintypes.LPCWSTR] | |
| kernel.CreateJobObjectW.restype = wintypes.HANDLE | |
| kernel.SetInformationJobObject.argtypes = [wintypes.HANDLE, ctypes.c_int, ctypes.c_void_p, wintypes.DWORD] | |
| kernel.AssignProcessToJobObject.argtypes = [wintypes.HANDLE, wintypes.HANDLE] | |
| kernel.CloseHandle.argtypes = [wintypes.HANDLE] | |
| handle = kernel.CreateJobObjectW(None, None) | |
| if not handle: | |
| raise ctypes.WinError(ctypes.get_last_error()) | |
| try: | |
| limits = ExtendedLimits() | |
| limits.basic.flags = 0x2000 # JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | |
| if not kernel.SetInformationJobObject(handle, 9, ctypes.byref(limits), ctypes.sizeof(limits)): | |
| raise ctypes.WinError(ctypes.get_last_error()) | |
| if not kernel.AssignProcessToJobObject(handle, wintypes.HANDLE(int(process._handle))): | |
| raise ctypes.WinError(ctypes.get_last_error()) | |
| except BaseException: | |
| kernel.CloseHandle(handle) | |
| raise | |
| process._owned_job = (kernel, handle) | |
| return process | |
| except BaseException: | |
| process.kill() | |
| process.wait(timeout=5) | |
| raise | |
| def release_owned(process): | |
| """Release the non-inheritable job handle after this child has exited.""" | |
| job = getattr(process, '_owned_job', None) | |
| if job: | |
| process._owned_job = None | |
| job[0].CloseHandle(job[1]) | |