0

I am trying to program a Python3 module that when run, will (1) start up a server on some localhost port, and (2) open the file being served on that server.

The main design decision for me at this point was having a function return the server's endpoint while keeping it running. I wanted something short and simple.

I had a few possibilities for a solution, such as generators and the other options listed in the "motivation" section of this PEP entry from this answer.

What seemed simplest to me was a run_server() function which would basically use a SimpleHTTPRequestHandler to serve at a specific port with a specific timeout. A start_server() function would the start a thread with run_server as its target, start the thread, and return the server's endpoint.

However, as a soon-to-be grad who is still learning proper usage of threading, I'm not sure that this is best practice/good design for what I am trying to achieve as far as threading is concerned. After having finished my undergrad, I look back at things I programmed in high school thinking they were brilliant solutions, horrified by how many best practice flaws/overcomplications they have.

Is this considered an OK thing to do as far as server startup/threading is concerned?

Lavie
  • 136
  • 7
  • If your objective is to have a function that returns the server's endpoint, generators seem like the wrong tool. They do not "return" a thing - they yield a sequence of things. The functions and classes you mention are, on the other hand, in the standard library. The standard docs provide examples of their use. So what you're asking is whether using the standard library is an OK thing to do. In my opinion, it's better than that: it's an excellent thing to do. Go for it. – Paul Cornelius Jul 19 '23 at 03:00

0 Answers0