Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW
Last active August 25, 2023 14:38
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 Hermann-SW/878672805f5ce139a072e227ad528289 to your computer and use it in GitHub Desktop.
Save Hermann-SW/878672805f5ce139a072e227ad528289 to your computer and use it in GitHub Desktop.
HTTP server: serves numbers in range for multi-host multi-core work distribution
# pylint: disable=C0103 (invalid-name)
"""
python range_server.py port start stop
"""
from socketserver import TCPServer
from http.server import SimpleHTTPRequestHandler
from threading import Thread
from timeit import default_timer
from time import strftime, gmtime
from sys import argv, stdout
port, start, stop = [int(a) for a in argv[1:]]
shutdown, nworkers, dt = [None, 0, 0]
class MyHttpRequestHandler(SimpleHTTPRequestHandler):
"""silence pylint"""
def do_GET(self):
global start, nworkers, dt # pylint: disable=W0603 (global-statement)
b = b""
if len(self.path) == 1:
nworkers += 1
if nworkers == 1:
dt = default_timer()
print(nworkers, "workers active")
stdout.flush()
else:
assert nworkers > 0
if self.path[1].isdigit():
print(self.path[1:])
stdout.flush()
if start < stop:
b = bytes(str(start), "utf-8")
start += 1
else:
nworkers -= 1
if nworkers == 0:
dt = default_timer() - dt
Thread(target=shutdown, daemon=True).start()
else:
print(nworkers, "workers remain")
stdout.flush()
self.send_response(200)
self.send_header("Content-length", len(b))
self.end_headers()
self.wfile.write(b)
def tstr(s):
"""format seconds"""
i = round(s)
if i >= 366 * 24 * 3600:
return ""
fmt = ""
if i >= 24 * 3600:
fmt = "%-jd "
i -= 24 * 3600
return "[" + strftime(fmt + "%H:%M:%S", gmtime(i)) + "]"
with TCPServer(("", port), MyHttpRequestHandler) as httpd:
shutdown = httpd.shutdown
httpd.serve_forever()
print(round(dt), "seconds", tstr(dt))
@Hermann-SW
Copy link
Author

Hermann-SW commented Aug 23, 2023

pi@raspberrypi4B2:~ $ black range_server.py
All done! ✨ 🍰 ✨
1 file left unchanged.
pi@raspberrypi4B2:~ $ pylint range_server.py 

--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)

pi@raspberrypi4B2:~ $ 

Works with range_client script, that is started after server:

hermann@7600x:~$ ./range_client raspberrypi4B2 9000 12
hermann@7600x:~$ 

range_server.py reports 2 found Cullen primes in range(4700, 5800):

pi@raspberrypi4B2:~ $ python range_server.py 9000 4700 5800 2>err | tee out
1 workers active
2 workers active
3 workers active
4 workers active
5 workers active
6 workers active
7 workers active
4713
8 workers active
9 workers active
10 workers active
11 workers active
12 workers active
11 workers remain
10 workers remain
9 workers remain
8 workers remain
7 workers remain
6 workers remain
5 workers remain
4 workers remain
3 workers remain
2 workers remain
5795
1 workers remain
2 seconds [00:00:02]
pi@raspberrypi4B2:~ $ 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment