mirror of
https://gitee.com/johng/gf
synced 2026-06-27 09:47:19 +08:00
gproc代码优化,示例程序改进
This commit is contained in:
@ -28,12 +28,6 @@ type Msg struct {
|
||||
Group string // 分组名称
|
||||
}
|
||||
|
||||
// TCP通信数据结构定义
|
||||
type sendQueueItem struct {
|
||||
Pid int // PID,发向哪个进程
|
||||
Data []byte // 数据
|
||||
}
|
||||
|
||||
// 获取指定进程的通信文件地址
|
||||
func getCommFilePath(pid int) string {
|
||||
return getCommDirPath() + gfile.Separator + gconv.String(pid)
|
||||
|
||||
@ -76,6 +76,7 @@ func tcpServiceHandler(conn *gtcp.Conn) {
|
||||
break
|
||||
}
|
||||
}
|
||||
// 成功时会返回ok给peer
|
||||
if len(result) == 0 {
|
||||
result = []byte("ok")
|
||||
for _, msg := range msgs {
|
||||
@ -129,9 +130,9 @@ func bufferToMsgs(buffer []byte) []*Msg {
|
||||
return msgs
|
||||
}
|
||||
|
||||
// 获取其他进程传递到当前进程的消息包,阻塞执行
|
||||
// 获取其他进程传递到当前进程的消息包,阻塞执行。
|
||||
func Receive(group...string) *Msg {
|
||||
// 开启端口监听
|
||||
// 开启接收协程时才会开启端口监听
|
||||
go startTcpListening()
|
||||
|
||||
var queue *gqueue.Queue
|
||||
|
||||
@ -16,6 +16,7 @@ import (
|
||||
"time"
|
||||
"bytes"
|
||||
"gitee.com/johng/gf/g/os/glog"
|
||||
"io"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -55,8 +56,8 @@ func Send(pid int, data []byte, group...string) error {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
// EOF不算异常错误
|
||||
if err == nil || err == io.EOF {
|
||||
break
|
||||
} else {
|
||||
glog.Error(err)
|
||||
|
||||
@ -1,15 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"fmt"
|
||||
"time"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func main () {
|
||||
cmd := exec.Command(os.Args[0], "1")
|
||||
time.Sleep(3*time.Second)
|
||||
fmt.Println(cmd.Start())
|
||||
time.Sleep(time.Hour)
|
||||
}
|
||||
@ -7,6 +7,7 @@ import (
|
||||
"gitee.com/johng/gf/g/os/gproc"
|
||||
)
|
||||
|
||||
// 父子进程基本演示
|
||||
func main () {
|
||||
if gproc.IsChild() {
|
||||
glog.Printfln("%d: Hi, I am child, waiting 3 seconds to die", gproc.Pid())
|
||||
|
||||
@ -5,6 +5,7 @@ import (
|
||||
"gitee.com/johng/gf/g/os/gproc"
|
||||
)
|
||||
|
||||
// 使用gproc kill指定其他进程(清确保运行该程序的用户有足够权限)
|
||||
func main () {
|
||||
pid := 28536
|
||||
m := gproc.NewManager()
|
||||
|
||||
@ -7,7 +7,8 @@ import (
|
||||
"gitee.com/johng/gf/g/os/gproc"
|
||||
)
|
||||
|
||||
// 父进程销毁后,请使用进程管理器查看存活的子进程
|
||||
// 父进程销毁后,使用进程管理器查看存活的子进程。
|
||||
// 请使用go build编译后运行,不要使用IDE运行,因为IDE大多采用的是子进程方式执行。
|
||||
func main () {
|
||||
if gproc.IsChild() {
|
||||
glog.Printfln("%d: I am child, waiting 10 seconds to die", gproc.Pid())
|
||||
|
||||
@ -8,7 +8,7 @@ import (
|
||||
"gitee.com/johng/gf/g/os/gproc"
|
||||
)
|
||||
|
||||
// 查看进程的环境变量
|
||||
// 查看父子进程的环境变量
|
||||
func main () {
|
||||
time.Sleep(5*time.Second)
|
||||
glog.Printfln("%d: %v", gproc.Pid(), genv.All())
|
||||
|
||||
@ -7,6 +7,6 @@ import (
|
||||
|
||||
// 执行shell指令
|
||||
func main () {
|
||||
r, err := gproc.ShellExec("echo 'hello';")
|
||||
r, err := gproc.ShellExec("sleep 3s; echo 'hello';")
|
||||
fmt.Println("result:", r, err)
|
||||
}
|
||||
Reference in New Issue
Block a user