I have this code (hello.py):
import os,sys
import tornado.ioloop
import tornado.web
import tornado.httpserver
#http server class
class http_server(tornado.web.RequestHandler):
def get(self):
self.write("Hello, getter!")
def post(self):
self.write("Hello, poster!")
#create http server
Handlers = [(r"/",http_server)]
App_Settings = {"debug":True}
HTTP_Server = tornado.web.Application(Handlers,**App_Settings)
#run http server
HTTP_Server.listen(9999)
tornado.ioloop.IOLoop.instance().start()
It runs fine for the first time with this command from terminal:
python hello.py
After this, the terminal keep waiting for output by Tornado. When i open http://localhost:9999, it returns “Hello,getter!” as wanted. But for the second time, Python shows an error:
Traceback (most recent call last):
File "hello.py", line 19, in <module>
HTTP_Server.listen(9999)
File "/usr/local/.../tornado/web.py", line 1227, in listen
server.listen(port, address)
File "/usr/local/.../tornado/netutil.py", line 100, in listen
sockets = bind_sockets(port, address=address)
File "/usr/local/.../tornado/netutil.py", line 265, in bind_sockets
sock.bind(sockaddr)
File "<string>", line 1, in bind
socket.error: [Errno 98] Address already in use
I press Ctrl-Z to send ‘hello.py’ to background, but this doesn’t release the port. If i close the terminal, the port is released. However the path to the python source file is deeply nested in directories, so i don’t want to close the terminal and restart it after every single change in the code. Some suggested to me that i should use debug=True in application setting but this doesn’t seem to be related to the matter that Tornado keeps holding the port.
I also tried “pidof python”, then kill all python processes but the port is still held by the stubborn Tornado. Is there any way to release all the ports held by Tornado with some Python statement? or manually?
Not really an answer to your question if you can release the Tornado ports using a Python statement, but since it seems to have solved your problem, I thought I’d write up a short answer anyway;
Ctrl-Zwill only put your program to sleep but not release any ports.If you on the other hand use
Ctrl-C, the program will be stopped entirely. That will allow you to simply use shell history to start it again.