簡體   English   中英

每當我在PyCharm中重新運行腳本時,都需要終止在端口上運行的進程

[英]Process running on port needs to be killed everytime I re-run a script in PyCharm

我目前正在Windows 10上學習CherryPy,在PyCharm上使用usnig Python 2.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

我已經看過了:

1. 如何在Windows中的localhost上殺死當前正在使用的端口?

2. 錯誤:該端口已在使用中。

但是我想知道的是一種在停止執行程序后立即關閉端口並關閉瀏覽器的方法。 這就是說,我希望端口關閉時,在其上執行的程序會自動關閉。 有什么辦法嗎? 任何幫助表示贊賞。 非常感謝。

PS:這是我正在運行的程序:

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

該端口可能正在使用中,因為程序仍在運行-程序的上一次調用正在使用該端口。

重新啟動程序之前,請確保完全退出該程序。

PyCharm有一個特點-單實例只有-這應該為你做這個。 我建議嘗試一下。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM