【问题标题】:Process running on port needs to be killed everytime I re-run a script in PyCharm每次我在 PyCharm 中重新运行脚本时,都需要终止在端口上运行的进程
【发布时间】:2018-12-11 08:33:08
【问题描述】:

我目前正在学习 CherryPy,在 PyCharm 上使用 Python 2.7,在 Windows 10 上。当我在上面执行一些程序时,我注意到了一些事情。我第一次运行程序它成功执行,并且所需的o/p出现在浏览器的localhost端口8080上。 但是,如果我在同一个程序中更改某些内容并再次运行它,我会得到:

C:\Python27\python.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:\Python27\lib\site-packages\cherrypy\process\wspbus.py", line 230, in publish
output.append(listener(*args, **kwargs))
  File "C:\Python27\lib\site-packages\cherrypy\_cpserver.py", line 191, in start
super(Server, self).start()
  File "C:\Python27\lib\site-packages\cherrypy\process\servers.py", line 177, in start
portend.free(*self.bind_addr, timeout=Timeouts.free)
  File "C:\Python27\lib\site-packages\portend.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:\Python27\lib\site-packages\cherrypy\process\wspbus.py", line 268, in start
self.publish('start')
  File "C:\Python27\lib\site-packages\cherrypy\process\wspbus.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

我已经看过了:

1.How to kill a currently using port on localhost in windows?

2.Error: That port is already in use.

但我想知道的是一种在我停止执行程序后立即关闭端口并关闭浏览器的方法。也就是说,我希望端口在其上执行的程序关闭时自动关闭。有没有办法做到这一点?任何帮助表示赞赏。提前非常感谢。

P.S:这是我正在运行的程序:

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())

【问题讨论】:

  • 您是否在重新启动程序之前停止程序?您是否尝试过在该程序的设置页面顶部勾选“仅限单个实例”?
  • 只勾选单个实例。并在关闭程序之前停止程序。感谢您指出@Shadow
  • 我已将我的评论升级为答案。如果它解决了您的问题 - 请考虑支持/接受它。
  • 使用快捷键Ctrl+F2停止程序运行。

标签: python pycharm


【解决方案1】:

该端口可能正在使用中,因为程序仍在运行 - 该端口正被您的程序的先前调用使用。

确保在重新启动程序之前完全退出程序。

PyCharm 有一个特性 - 仅限单实例 - 应该为您执行此操作。我建议尝试一下。

【讨论】: