Process - Communication - 图1danger

    The inter-process communication feature provided by the gproc component is experimental!

    Do not communicate by sharing memory; instead, share memory by communicating.

    There are 5 common methods of inter-process communication: pipe/signal/shared memory/shared file/Socket, each typically having its preferred usage scenarios.

    • Signal: Signals are commonly used in *nix systems, with poor cross-platform capabilities and simple methods and contents for information transmission.
    • Pipe: Including ordinary pipes and named pipes, this method is commonly used in parent-child process communication scenarios and is not very suitable for communication between unrelated processes.
    • Shared Memory/Shared File: In terms of concurrent architecture design, we try to minimize the use of lock mechanisms, including shared memory (memory locks)/shared files (file locks), which actually require lock mechanisms to ensure the correctness of data flow. The maintenance complexity often outweighs the benefits brought by the lock mechanisms.

    The primary mechanism that gproc implements for inter-process communication is Socket, which has the advantage of functionality stability and general usage scenarios.

    The API for inter-process communication in gproc is extremely simple, achieved through the following two methods:

    1. func Send(pid int, data []byte) error
    2. func Receive() *Msg

    We use the Send method to send data to a specified process (each call is equivalent to sending a message), and in the specified process, we can obtain the data through the Receive method. The Receive method offers a message queue-like approach to receive data from other processes. It will block and wait when the queue is empty.

    Let’s look at a basic usage example of inter-process communication:

    1. package main
    2. import (
    3. "context"
    4. "fmt"
    5. "github.com/gogf/gf/v2/os/gctx"
    6. "github.com/gogf/gf/v2/os/gproc"
    7. "github.com/gogf/gf/v2/os/gtime"
    8. "github.com/gogf/gf/v2/os/gtimer"
    9. "os"
    10. "time"
    11. )
    12. var (
    13. ctx = gctx.New()
    14. )
    15. func main() {
    16. fmt.Printf("%d: I am child? %v\n", gproc.Pid(), gproc.IsChild())
    17. if gproc.IsChild() {
    18. gtimer.SetInterval(ctx, time.Second, func(ctx context.Context) {
    19. err := gproc.Send(gproc.PPid(), []byte(gtime.Datetime()))
    20. if err != nil {
    21. return
    22. }
    23. })
    24. select {}
    25. } else {
    26. m := gproc.NewManager()
    27. p := m.NewProcess(os.Args[0], os.Args, os.Environ())
    28. p.Start(ctx)
    29. for {
    30. msg := gproc.Receive()
    31. fmt.Printf("receive from %d, data: %s\n", msg.SenderPid, string(msg.Data))
    32. }
    33. }
    34. }

    In this example, our main process creates a child process upon startup. The child process sends the current time to the main process every second, and the main process outputs the received parameters from the child process to the terminal. After execution, the content output in the terminal is as follows:

    1. 29978: I am child? false
    2. 29984: I am child? true
    3. receive from 29984, data: 2018-05-18 15:01:00
    4. receive from 29984, data: 2018-05-18 15:01:01
    5. receive from 29984, data: 2018-05-18 15:01:02
    6. receive from 29984, data: 2018-05-18 15:01:03
    7. receive from 29984, data: 2018-05-18 15:01:04
    8. ...