/
unicorn.py
67 lines (59 loc) · 2.25 KB
/
unicorn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
Simple preforking echo server in Python.
Python port of http://tomayko.com/writings/unicorn-is-unix.
"""
import os
import sys
import socket
# Create a socket, bind it to localhost:4242, and start
# listening. Runs once in the parent; all forked children
# inherit the socket's file descriptor.
acceptor = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
acceptor.bind(('localhost', 4242))
acceptor.listen(10)
# Ryan's Ruby code here traps EXIT and closes the socket. This
# isn't required in Python; the socket will be closed when the
# socket object gets garbage collected.
# Fork you some child processes. In the parent, the call to
# fork returns immediately with the pid of the child process;
# fork never returns in the child because we exit at the end
# of the block.
for i in range(3):
pid = os.fork()
# os.fork() returns 0 in the child process and the child's
# process id in the parent. So if pid == 0 then we're in
# the child process.
if pid == 0:
# now we're in the child process; trap (Ctrl-C)
# interrupts by catching KeyboardInterrupt) and exit
# immediately instead of dumping stack to stderr.
childpid = os.getpid()
print("Child %s listening on localhost:4242" % childpid)
try:
while True:
# This is where the magic happens. accept(2)
# blocks until a new connection is ready to be
# dequeued.
conn, addr = acceptor.accept()
# For easier use, turn the socket connection
# into a file-like object.
flo = conn.makefile(mode='rw')
flo.write('Child %s echo> ' % childpid)
flo.flush()
message = flo.readline()
flo.write(message)
flo.close()
conn.close()
print("Child %s echo'd: %r" % (childpid, message.strip()))
except KeyboardInterrupt:
sys.exit()
# Sit back and wait for all child processes to exit.
#
# Trap interrupts, write a note, and exit immediately in
# parent. This trap is not inherited by the forks because it
# runs after forking has commenced.
try:
os.waitpid(-1, 0)
except KeyboardInterrupt:
print("\nbailing")
sys.exit()