Kill ListenAndServe in GoLang

huangapple go评论101阅读模式
英文:

Kill ListenAndServe in GoLang

问题

我正在尝试使用Sublime Text + GoSublime进行Golang开发。

我正在使用Go编写一个Web服务器(ListenAndServe("8000", &handler))。

我的问题是,当我运行Go进程时(同时另一个进程正在运行),第二个进程无法工作(因为端口已经被占用)。

我希望的情况是,当我运行go run web.go时,如果已经有一个进程在运行,先关闭它,然后再运行新的进程。

Sublime Text 3的构建系统似乎无法跟踪先前的执行情况 - 所以我不知道最好的终止方式。当我按下CTRL+B时,它不会阻塞,而是在另一个线程中执行构建系统 - 这意味着在执行下一个构建之前我无法按下CTRL+C。所以我现在需要在命令行中终止现有进程,然后再启动下一个进程。

因此,我正在寻找Sublime Text的构建系统配置的解决方案,或者甚至让Go进程自己检查是否有进程在运行并终止它。

有什么想法吗?在终端和Sublime Text之间来回切换有点麻烦。

英文:

I am trying to use Sublime Text + GoSublime to do Golang development.

I am working with a webserver in Go (ListenAndServe("8000", &handler)).

The problem I have is that when I run the Go process (while another one is running), the second process doesn't work (since the port is already in use).

What I want to happen is that when I go run web.go, if one was already running, shut that down before running this one.

Sublime Text 3's build system doesn't seem to keep track of the previous execution - so I don't know the best way to terminate. When I run CTRL+B it doesn't block, it executes the build system in another thread - meaning that I can't CTRL+C before executing the next. So what I'm having to do is terminate via the command line the existing process before starting the next one.

So I'm looking for either a solution with Sublime Text's build system configuration, or perhaps even having the Go process itself check to see if one is running and killing the process.

Ideas? It's sort of annoying to swap back and forth from terminal.

答案1

得分: 5

不确定我是否正确理解了问题。

但是在Sublime Text的构建控制台中,有一个名为tskill的命令,可以终止从控制台启动的活动任务。因此,您可以按下Ctrl+B调出控制台,然后输入tskill来终止正在运行的web.go任务。还有其他别名,如tskill <任务ID>task replaytask go。参考链接在这里。之后,您可以运行其他命令,包括go run

此外,还有一个replay命令。它类似于run命令,但在运行之前会尝试取消任何活动的执行。我觉得这看起来就是你想要的。

此外,Gosublime命令行还有一个sh命令模式。它更加灵活,例如可以输入sh killall web;go run web.go或运行一个shell脚本来运行它。

希望对您有所帮助。

英文:

Not sure if I understand the question right.

But in sublime text build console, there is tskill command which can kill active tasks initiated from the console. So you can type Ctrl+B to invoke out console and type tskill to kill the running web.go task. There are also other alias tskill &lt;task ID&gt;, task replay or task go. Reference is here. After that you can run other commands including go run.

What's more there is a replay command. "It is like run but attempts to cancel any active executions before running". I feel it looks like what you want.

Further, Gosublime command shell has sh command mode. It's much flexible for example type sh killall web;go run web.go or run a shell script to run that.

Hopefully it helps.

huangapple
  • 本文由 发表于 2014年3月21日 02:38:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/22542212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定