Skip to content

Instantly share code, notes, and snippets.

@graingert
Created July 7, 2022 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graingert/7a29d08dd06f571402c503bc1868fc3b to your computer and use it in GitHub Desktop.
Save graingert/7a29d08dd06f571402c503bc1868fc3b to your computer and use it in GitHub Desktop.
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