-
-
Save graingert/7a29d08dd06f571402c503bc1868fc3b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import psutil | |
import multiprocessing | |
import socket | |
import sys | |
import concurrent.futures | |
ctx = multiprocessing.get_context("spawn") | |
def target(sock): | |
with sock: | |
sock.send(b"\x00") | |
sock.recv(1) | |
def main(): | |
def cb(): | |
proc.join() | |
return proc.exitcode | |
with concurrent.futures.ThreadPoolExecutor() as tpe: | |
while True: | |
a, b = socket.socketpair() | |
with a, b: | |
with b: | |
proc = ctx.Process(target=target, args=(b, )) | |
proc.start() | |
join_fut = tpe.submit(cb) | |
assert a.recv(1) == b"\00" | |
psutil_proc = psutil.Process(proc.pid) | |
psutil_proc.kill() | |
psutil_proc.wait() | |
assert a.recv(1) == b"" | |
print(f"{psutil_proc=}") | |
print(f"{proc=}") | |
join_fut_result = join_fut.result() | |
print(f"{join_fut_result=}") | |
if join_fut_result is None: | |
return "broken!" | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment