Skip to content

Commit

Permalink
add: some test file
Browse files Browse the repository at this point in the history
  • Loading branch information
yihong0618 committed Apr 24, 2020
1 parent a3b5283 commit f6571a8
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 0 deletions.
6 changes: 6 additions & 0 deletions learn_ml/.ipynb_checkpoints/Untitled-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
}
6 changes: 6 additions & 0 deletions learn_ml/Untitled.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
}
18 changes: 18 additions & 0 deletions re_learn_python/fork_learn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os

a = 2
pid = os.fork()
pid = os.fork()
print(a, pid, os.getpid())
# import os
# import time

# for i in range(2):
# print("I'm about to be a dad!")
# time.sleep(5)
# pid = os.fork()
# if pid == 0:
# print("I'm {}, a newborn that knows to write to the terminal!".format(os.getpid()))
# else:
# print("I'm the dad of {}, and he knows to use the terminal!".format(pid))
# os.waitpid(pid, 0)
26 changes: 26 additions & 0 deletions re_learn_python/test_s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import socket
import time
import os

RESPONSE = """\
HTTP/1.1 200 OK
<H1>Hello world{}</H1>""".replace("\n", "\r\n")


with socket.socket() as s:
s.bind(("127.0.0.1", 5434))
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.listen(5)
while True:
conn, addr = s.accept()
pid = os.fork()
if pid == 0:
s.close()
conn.sendall(RESPONSE.format(os.getpid()).encode())
time.sleep(10)
conn.close()
os._exit(0)
else:
conn.close()

56 changes: 56 additions & 0 deletions re_learn_python/test_s2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
###########################################################################
# Concurrent server - webserver3c.py #
# #
# Tested with Python 2.7.9 & Python 3.4 on Ubuntu 14.04 & Mac OS X #
# #
# - Child process sleeps for 60 seconds after handling a client's request #
# - Parent and child processes close duplicate descriptors #
# #
###########################################################################
import os
import socket
import time

SERVER_ADDRESS = (HOST, PORT) = '', 8888
REQUEST_QUEUE_SIZE = 5


def handle_request(client_connection):
request = client_connection.recv(1024)
print(
'Child PID: {pid}. Parent PID {ppid}'.format(
pid=os.getpid(),
ppid=os.getppid(),
)
)
print(request.decode())
http_response = b"""\
HTTP/1.1 200 OK
Hello, World!
""".replace(b"\n", b"\r\n")
client_connection.sendall(http_response)
time.sleep(60)


def serve_forever():
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_socket.bind(SERVER_ADDRESS)
listen_socket.listen(REQUEST_QUEUE_SIZE)
print('Serving HTTP on port {port} ...'.format(port=PORT))
print('Parent PID (PPID): {pid}\n'.format(pid=os.getpid()))

while True:
client_connection, client_address = listen_socket.accept()
pid = os.fork()
if pid == 0: # child
listen_socket.close() # close child copy
handle_request(client_connection)
client_connection.close()
os._exit(0) # child exits here
else: # parent
client_connection.close() # close parent copy and loop over

if __name__ == '__main__':
serve_forever()
3 changes: 3 additions & 0 deletions t.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import os

print(os.fork())

0 comments on commit f6571a8

Please sign in to comment.