Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 每次我在PyCharm中重新运行脚本时,都需要终止端口上运行的进程_Python_Pycharm - Fatal编程技术网

Python 每次我在PyCharm中重新运行脚本时,都需要终止端口上运行的进程

Python 每次我在PyCharm中重新运行脚本时,都需要终止端口上运行的进程,python,pycharm,Python,Pycharm,我目前正在学习CherryPy,在Windows10上的PyCharm上的USNIGPython2.7。当我在上面执行一些程序时,我注意到了一些东西。第一次运行程序时,它成功执行,并且所需的o/p显示在浏览器中的localhost端口8080上。 但是,如果我在同一个程序中更改某些内容并再次运行,我会得到以下结果: C:\Python27\python.exe C:/Users/ymodak/Desktop/Training/x.py [03/Jul/2018:11:15:37] ENGINE

我目前正在学习CherryPy,在Windows10上的PyCharm上的USNIGPython2.7。当我在上面执行一些程序时,我注意到了一些东西。第一次运行程序时,它成功执行,并且所需的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
C:\Python27\python.exe C:/Users/ymodak/Desktop/Training/x.py
[2018年7月3日:11:15:37]发动机正在收听SIGTERM。
[2018年7月3日:11:15:37]发动机总线启动
樱桃色棋盘格:
在“”上装载的应用程序具有空配置。
[03/Jul/2018:11:15:37]控制台事件的引擎集处理程序。
[03/Jul/2018:11:15:37]引擎已启动监视器线程“Autoreloader”。
[03/Jul/2018:11:15:39]在“启动”侦听器中出现发动机错误
回溯(最近一次呼叫最后一次):
文件“C:\Python27\lib\site packages\cherrypy\process\wspbus.py”,第230行,在publish中
append(侦听器(*args,**kwargs))
文件“C:\Python27\lib\site packages\cherrypy\\u cpserver.py”,第191行,在开始处
super(Server,self).start()
文件“C:\Python27\lib\site packages\cherrypy\process\servers.py”,第177行,在开始处
portend.free(*self.bind\u addr,timeout=Timeouts.free)
文件“C:\Python27\lib\site packages\portend.py”,第119行,免费
提升超时(“端口{Port}在{host}上不可用。”.format(**locals())
超时:端口8080在127.0.0.1上不可用。
[03/Jul/2018:11:15:39]由于启动侦听器出错,发动机关闭:
回溯(最近一次呼叫最后一次):
文件“C:\Python27\lib\site packages\cherrypy\process\wspbus.py”,第268行,在开始处
self.publish('start')
文件“C:\Python27\lib\site packages\cherrypy\process\wspbus.py”,第248行,在publish中
加薪
ChannelFailures:超时('端口8080在127.0.0.1上不可用',)
[2018年7月3日:11:15:39]发动机总线停止
[03/Jul/2018:11:15:39]引擎HTTP服务器
cherrypy._cpwsgi_server.CPWSGIServer(('127.0.0.1',8080))已关闭
[2018年7月3日:11:15:39]发动机已停止线程“Autoreloader”。
[03/Jul/2018:11:15:39]引擎已删除控制台事件的处理程序。
[2018年7月3日:11:15:39]发动机总线停止
[2018年7月3日:11:15:39]发动机总线退出
[2018年7月3日:11:15:39]发动机总线退出
进程已完成,退出代码为70
我已经看过:

一,

二,

但我想知道的是一种在我停止程序执行后立即关闭端口并关闭浏览器的方法。也就是说,我希望端口在其上执行的程序关闭时自动关闭。有办法吗?感谢您的帮助。先谢谢你

这是我正在运行的程序:

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())
随机导入
导入字符串
进口樱桃
类StringGenerator(对象):
@樱桃树
def索引(自):
返回“”
生成
"""
@樱桃树
def生成(自身,长度=8):
返回“”。连接(random.sample(string.hexdigits,int(长度)))
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
cherrypy.quickstart(StringGenerator())

端口可能正在使用中,因为程序仍在运行-该端口正由程序的上一次调用使用

重新启动程序之前,请确保完全退出该程序


PyCharm有一个特性—仅限单个实例—可以为您实现这一点。我建议您尝试一下。

您是否在重新启动程序之前停止该程序?您是否尝试过在该程序的设置页面顶部勾选“仅单个实例”?勾选“仅单个实例”。在关闭程序之前停止它是有效的。谢谢你指出,我已经把我的评论升级为答案了。如果它解决了你的问题-请考虑接受/接受它。使用快捷方式CTRL+F2停止程序的运行。