如何使用上下文
How to use context
对于个人项目,我生成了一个托管 REST API 的外部进程。
我只想在外部进程初始化后将控制权传回主线程,这可以通过读取 Stdout 知道。
所以我创建了一个 io.Writer
实现,它会在满足特定条件后关闭频道。
type channelIOWriter struct {
InitializedChannel chan bool
isInitialized bool
buffer string
}
func newChannelIOWriter() *channelIOWriter {
retVal := new(channelIOWriter)
retVal.InitializedChannel = make(chan bool)
return retVal
}
func (writer *channelIOWriter) Write(data []byte) (int, error) {
for _, b := range data {
if b == '\n' {
writer.buffer = *bytes.NewBuffer(make([]byte, 0))
continue
}
writer.buffer.WriteByte(b)
if strings.HasPrefix(writer.buffer.String(), "ChromeDriver was started successfully.") {
if !writer.isInitialized {
close(writer.InitializedChannel)
}
writer.isInitialized = true
}
}
return len(data), nil
}
接下来,我有 main 函数,它在一个单独的 goroutine 中生成外部进程。
在应该由 io.Writer
实现关闭的通道上执行等待。
func main() {
writer := newChannelIOWriter()
go func() {
cmd := exec.Command("chromedriver", "--port=9009")
cmd.Stdout = writer
cmd.Start()
}()
<-writer.InitializedChannel
fmt.Println("Control is passed back to the MAIN thread.")
}
根据问题的评论,似乎使用上下文更好。
谁能解释一下如何使用它?
我删除了所有不必要的细节并演示了具有截止日期的上下文如何工作
package main
import (
"context"
"fmt"
"os/exec"
"time"
)
type example struct {
cancel context.CancelFunc
}
func (e *example) Write(data []byte) (int, error) {
defer e.cancel()
return len(data), nil
}
func main() {
deadline := time.Now().Add(5 * time.Second)
ctx, cancel := context.WithDeadline(context.Background(), deadline)
writer := &example{
cancel: cancel,
}
go func() {
cmd := exec.Command("ls", ".")
cmd.Stdout = writer
_ = cmd.Start()
}()
<-ctx.Done()
fmt.Println("Control is passed back to the MAIN thread.", ctx.Err())
}
对于个人项目,我生成了一个托管 REST API 的外部进程。 我只想在外部进程初始化后将控制权传回主线程,这可以通过读取 Stdout 知道。
所以我创建了一个 io.Writer
实现,它会在满足特定条件后关闭频道。
type channelIOWriter struct {
InitializedChannel chan bool
isInitialized bool
buffer string
}
func newChannelIOWriter() *channelIOWriter {
retVal := new(channelIOWriter)
retVal.InitializedChannel = make(chan bool)
return retVal
}
func (writer *channelIOWriter) Write(data []byte) (int, error) {
for _, b := range data {
if b == '\n' {
writer.buffer = *bytes.NewBuffer(make([]byte, 0))
continue
}
writer.buffer.WriteByte(b)
if strings.HasPrefix(writer.buffer.String(), "ChromeDriver was started successfully.") {
if !writer.isInitialized {
close(writer.InitializedChannel)
}
writer.isInitialized = true
}
}
return len(data), nil
}
接下来,我有 main 函数,它在一个单独的 goroutine 中生成外部进程。
在应该由 io.Writer
实现关闭的通道上执行等待。
func main() {
writer := newChannelIOWriter()
go func() {
cmd := exec.Command("chromedriver", "--port=9009")
cmd.Stdout = writer
cmd.Start()
}()
<-writer.InitializedChannel
fmt.Println("Control is passed back to the MAIN thread.")
}
根据问题的评论,似乎使用上下文更好。 谁能解释一下如何使用它?
我删除了所有不必要的细节并演示了具有截止日期的上下文如何工作
package main
import (
"context"
"fmt"
"os/exec"
"time"
)
type example struct {
cancel context.CancelFunc
}
func (e *example) Write(data []byte) (int, error) {
defer e.cancel()
return len(data), nil
}
func main() {
deadline := time.Now().Add(5 * time.Second)
ctx, cancel := context.WithDeadline(context.Background(), deadline)
writer := &example{
cancel: cancel,
}
go func() {
cmd := exec.Command("ls", ".")
cmd.Stdout = writer
_ = cmd.Start()
}()
<-ctx.Done()
fmt.Println("Control is passed back to the MAIN thread.", ctx.Err())
}