Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SocketServer continue execution after serve_forever

Tags:

python

I'm using the SocketServer module in python to implement a simple embedded web server in my application. At the moment I'm calling the serve_forver method within a dedicated thread in order to continue the execution of my application.

I was wondering if there is there a more pythonic way to start my SocketServer and retun the execution to my application.

like image 749
Roberto Avatar asked Jul 10 '11 10:07

Roberto


1 Answers

That is a very pythonic way:

threading.Thread(target=myServer.serve_forever).start()

I don't see how that could be unclear or overly verbose. If you want the program to exit once your main thread finishes, add daemon=True to the Thread call.

An alternative is calling handle_request in a loop yourself, but that doesn't seem applicable to your case of a web-server.

like image 106
phihag Avatar answered Sep 29 '22 11:09

phihag