| 1 |
import os |
| 2 |
import signal |
| 3 |
import sys |
| 4 |
import tempfile |
| 5 |
__test__ = False |
| 6 |
|
| 7 |
|
| 8 |
def parent(signal_path, pid): |
| 9 |
eventlet.Timeout(5) |
| 10 |
port = None |
| 11 |
while True: |
| 12 |
try: |
| 13 |
contents = open(signal_path, 'rb').read() |
| 14 |
port = int(contents.strip()) |
| 15 |
break |
| 16 |
except Exception: |
| 17 |
eventlet.sleep(0.1) |
| 18 |
eventlet.connect(('127.0.0.1', port)) |
| 19 |
while True: |
| 20 |
try: |
| 21 |
contents = open(signal_path, 'rb').read() |
| 22 |
result = contents.split()[1] |
| 23 |
break |
| 24 |
except Exception: |
| 25 |
eventlet.sleep(0.1) |
| 26 |
assert result == b'done', repr(result) |
| 27 |
print('pass') |
| 28 |
|
| 29 |
|
| 30 |
def child(signal_path): |
| 31 |
eventlet.Timeout(5) |
| 32 |
s = eventlet.listen(('127.0.0.1', 0)) |
| 33 |
with open(signal_path, 'wb') as f: |
| 34 |
f.write(str(s.getsockname()[1]).encode() + b'\n') |
| 35 |
f.flush() |
| 36 |
s.accept() |
| 37 |
f.write(b'done\n') |
| 38 |
f.flush() |
| 39 |
|
| 40 |
|
| 41 |
if __name__ == '__main__': |
| 42 |
import eventlet |
| 43 |
|
| 44 |
with tempfile.NamedTemporaryFile() as signal_file: |
| 45 |
signal_path = signal_file.name |
| 46 |
|
| 47 |
pid = os.fork() |
| 48 |
if pid < 0: |
| 49 |
sys.stderr.write('fork error\n') |
| 50 |
sys.exit(1) |
| 51 |
elif pid == 0: |
| 52 |
child(signal_path) |
| 53 |
sys.exit(0) |
| 54 |
elif pid > 0: |
| 55 |
try: |
| 56 |
parent(signal_path, pid) |
| 57 |
except Exception: |
| 58 |
os.kill(pid, signal.SIGTERM) |