Process running on port needs to be killed everytime I re-run a script in PyCharm
Process running on port needs to be killed everytime I re-run a script in PyCharm
I'm currently learning CherryPy, usnig Python 2.7 on PyCharm, on Windows 10. While I executed a few programs on it, I noticed something. The first time I run the program it successfully executes, and the required o/p appears on localhost port 8080 in the browser.
However, if I change something in the same program and run it again, I get this:
C:Python27python.exe C:/Users/ymodak/Desktop/Training/x.py
[03/Jul/2018:11:15:37] ENGINE Listening for SIGTERM.
[03/Jul/2018:11:15:37] ENGINE Bus STARTING
CherryPy Checker:
The Application mounted at '' has an empty config.
[03/Jul/2018:11:15:37] ENGINE Set handler for console events.
[03/Jul/2018:11:15:37] ENGINE Started monitor thread 'Autoreloader'.
[03/Jul/2018:11:15:39] ENGINE Error in 'start' listener <bound method
Server.start of <cherrypy._cpserver.Server object at 0x0000000003B14F98>>
Traceback (most recent call last):
File "C:Python27libsite-packagescherrypyprocesswspbus.py", line 230, in publish
output.append(listener(*args, **kwargs))
File "C:Python27libsite-packagescherrypy_cpserver.py", line 191, in start
super(Server, self).start()
File "C:Python27libsite-packagescherrypyprocessservers.py", line 177, in start
portend.free(*self.bind_addr, timeout=Timeouts.free)
File "C:Python27libsite-packagesportend.py", line 119, in free
raise Timeout("Port {port} not free on {host}.".format(**locals()))
Timeout: Port 8080 not free on 127.0.0.1.
[03/Jul/2018:11:15:39] ENGINE Shutting down due to error in start listener:
Traceback (most recent call last):
File "C:Python27libsite-packagescherrypyprocesswspbus.py", line 268, in start
self.publish('start')
File "C:Python27libsite-packagescherrypyprocesswspbus.py", line 248, in publish
raise exc
ChannelFailures: Timeout('Port 8080 not free on 127.0.0.1.',)
[03/Jul/2018:11:15:39] ENGINE Bus STOPPING
[03/Jul/2018:11:15:39] ENGINE HTTP Server
cherrypy._cpwsgi_server.CPWSGIServer(('127.0.0.1', 8080)) already shut down
[03/Jul/2018:11:15:39] ENGINE Stopped thread 'Autoreloader'.
[03/Jul/2018:11:15:39] ENGINE Removed handler for console events.
[03/Jul/2018:11:15:39] ENGINE Bus STOPPED
[03/Jul/2018:11:15:39] ENGINE Bus EXITING
[03/Jul/2018:11:15:39] ENGINE Bus EXITED
Process finished with exit code 70
I have already looked at:
1.How to kill a currently using port on localhost in windows?
2.Error: That port is already in use.
But what I want to know is a way to shut the port immediately after I stop the execution of the program, and close the browser. Which is to say, I want the port to close automatically when the program executed on it is shut down. Is there any way to do that? Any help is appreciated. Thanks a lot in advance.
P.S: This is the program I'm running:
import random
import string
import cherrypy
class StringGenerator(object):
@cherrypy.expose
def index(self):
return """<html>
<head></head>
<body>
<form method="put" action="generate">
<input type="text" value="8" name="length" />
<button type="submit">Generate!</button>
</form>
</body>
</html>"""
@cherrypy.expose
def generate(self, length=8):
return ''.join(random.sample(string.hexdigits, int(length)))
if __name__ == '__main__':
cherrypy.quickstart(StringGenerator())
Single instance only is ticked. And stopping the program before closing it worked. thanks for pointing it out @Shadow
– Yogesh Sudheer Modak
Jul 3 at 6:22
I've upgraded my comment to an answer. If it solves your problem - please consider upvoting/accepting it.
– Shadow
Jul 3 at 6:26
Used the shortcut Ctrl+F2 to stop the running of the program.
– Yogesh Sudheer Modak
Jul 3 at 6:30
1 Answer
1
The port is probably in use because the program is still running - the port is in use by the previous invocation of your program.
Make sure you fully exit the program before restarting it.
PyCharm has a feature - Single Instance Only - that should do this for you. I'd recommend giving that a try.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Are you stopping the program before starting it again? Have you tried ticking "single instance only" at the top of that program's setup page?
– Shadow
Jul 3 at 6:19