socketify.py/examples/forks.py

31 wiersze
619 B
Python
Czysty Zwykły widok Historia

2022-05-31 21:51:39 +00:00
from socketify import App
import os
import multiprocessing
2022-11-16 19:28:46 +00:00
2022-05-31 21:51:39 +00:00
def run_app():
app = App()
app.get("/", lambda res, req: res.end("Hello, World!"))
2022-11-16 19:28:46 +00:00
app.listen(
3000,
lambda config: print(
"PID %d Listening on port http://localhost:%d now\n"
% (os.getpid(), config.port)
),
)
2022-05-31 21:51:39 +00:00
app.run()
2022-11-16 19:28:46 +00:00
2022-05-31 21:51:39 +00:00
def create_fork():
n = os.fork()
# n greater than 0 means parent process
if not n > 0:
run_app()
2022-11-16 19:28:46 +00:00
2022-05-31 21:51:39 +00:00
# fork limiting the cpu count - 1
for i in range(1, multiprocessing.cpu_count()):
create_fork()
2022-11-16 19:28:46 +00:00
run_app() # run app on the main process too :)