当前位置: 首页 > news >正文

绍兴做网站的最近新闻今日头条

绍兴做网站的,最近新闻今日头条,滕州哪里有做网站的,游戏公司官方网站建设方案问题描述: 在go程序中,通过执行一个命令启动一个子命令,并通过pipe读取子程序的标准输入和输出,通过scanner默认按行读取,此时如果子程序输出时没有携带’\n’,scanner就不会打印输出,而是会累…

问题描述:

在go程序中,通过执行一个命令启动一个子命令,并通过pipe读取子程序的标准输入和输出,通过scanner默认按行读取,此时如果子程序输出时没有携带’\n’,scanner就不会打印输出,而是会累积到缓存buf上限,最终被丢弃,直到遇到一个\n,然后输出所有的内容,默认buf缓存上限时65536,如果日志打印处还有限制,如glog就限制最大的打印字节数为4096,那么就会导致日志再次丢失。

解决方法:

不适用scanner去按行读取,直接读取管道的内容,然后设置上限,超过时或者遇到’\n’时打印

测试代码:

子程序:

#include <stdio.h>
#include <unistd.h>int main() {
int count = 0;while (1) {fprintf(stderr, "%d", count);count = (count + 1) % 10;usleep(500); // Sleep for 500,000 microseconds (0.5 seconds)
}return 0;
}

主程序:

package mainimport ("bufio""fmt""os/exec""strings""log"
)func main() {cmd := exec.Command("./test")stdout, err := cmd.StdoutPipe()if err != nil {fmt.Println("Error creating StdoutPipe:", err)return}cmd.Stderr = cmd.Stdouterr = cmd.Start()if err != nil {fmt.Println("Error starting command:", err)return}scanner := bufio.NewScanner(stdout)// scanner.Split(bufio.ScanBytes)// buf := ""// for scanner.Scan() {// 	   buf += scanner.Text()//     if strings.Contains(buf, "\n") || len(buf) >= 256 {//         log.Printf("%s", buf)//         buf = ""//     }// }for scanner.Scan() {log.Printf("%s", scanner.Text())}if err := scanner.Err(); err != nil {fmt.Println("Error reading standard output:", err)}err = cmd.Wait()if err != nil {fmt.Println("Error waiting for command to finish:", err)}
}

修改程序:

package mainimport ("bufio""fmt""io""log""os/exec"
)func getReaderSize(rd io.Reader) {b, ok := rd.(*bufio.Reader)if ok {log.Printf("rd size: %d", b.Size())} else {log.Printf("rd is not bufio.Reader")}
}func main() {// Command to executecmd := exec.Command("./test")// Create a pipe to capture the standard output of the commandstdout, err := cmd.StdoutPipe()if err != nil {fmt.Println("Error creating StdoutPipe:", err)return}cmd.Stderr = cmd.Stdout// Start the commanderr = cmd.Start()if err != nil {fmt.Println("Error starting command:", err)return} Create a scanner to read the command's standard output//scanner := bufio.NewScanner(stdout)//scanner.Split(bufio.ScanBytes)// Read and print each line from the output//buf := make([]byte, 256)//bufLen := 0//for scanner.Scan() {//	buf[bufLen] = scanner.Bytes()[0]//	// buf = append(buf, scanner.Bytes()...)//	bufLen += 1//	if buf[bufLen-1] == '\n' || bufLen >= 256 {//		log.Printf("%s", string(buf[:bufLen]))//		bufLen = 0//	}//}// Check for errors in scanning//if err := scanner.Err(); err != nil {//	fmt.Println("Error reading standard output:", err)//}// Create a buffered reader to read from the command's stdoutreader := bufio.NewReaderSize(stdout, 256)getReaderSize(stdout)log.Printf("reader size: %d", reader.Size()) Buffer to store incomplete lines//var incompleteLine []byte// Buffer to read chunks of bytes//chunk := make([]byte, 256)////for {//	// Read a chunk of bytes//	n, err := reader.Read(chunk)//	if err != nil {//		break // Break the loop when an error occurs (e.g., when the command finishes)//	}////	// Process each byte in the chunk//	for i := 0; i < n; i++ {//		b := chunk[i]////		// Check for newline or length exceeding 256//		if b == '\n' || len(incompleteLine) >= 256 {//			// Print the line//			log.Printf("%s", incompleteLine)////			// Reset the incomplete line buffer//			incompleteLine = nil//		} else {//			// Add the byte to the incomplete line buffer//			incompleteLine = append(incompleteLine, b)//		}//	}//}for {s, err := reader.ReadSlice('\n')if err != nil && err != bufio.ErrBufferFull {if len(s) > 0 {log.Printf("reader err but exist data, reader size: %d, read string size: %d, string: %s", reader.Size(), len(s), string(s))}fmt.Println("Error reader ReadString:", err)break // Break the loop when an error occurs (e.g., when the command finishes)}log.Printf("reader size: %d, read string size: %d, string: %s", reader.Size(), len(s), string(s))}// Wait for the command to finisherr = cmd.Wait()if err != nil {fmt.Println("Error waiting for command to finish:", err)}
}

benchmark test:

package mainimport ("strconv""strings""testing"
)func stringTest1() string {var buf stringfor i := 0; i < 256; i++ {buf += strconv.Itoa(i)}return buf
}func stringTest2() string {var buf strings.Builderfor i := 0; i < 256; i++ {buf.Write([]byte(strconv.Itoa(i)))}return buf.String()
}func stringTest3() string {var buf = make([]byte, 0)for i := 0; i < 256; i++ {buf = append(buf, []byte(strconv.Itoa(i))...)}return string(buf)
}func stringTest4() string {var buf = make([]byte, 256)for i := 0; i < 256; i++ {buf[i] = '1'}return string(buf)
}func BenchmarkStringTest1(b *testing.B) {for i := 0; i < b.N; i++ {stringTest1()}
}
func BenchmarkStringTest2(b *testing.B) {for i := 0; i < b.N; i++ {stringTest2()}
}
func BenchmarkStringTest3(b *testing.B) {for i := 0; i < b.N; i++ {stringTest3()}
}
func BenchmarkStringTest4(b *testing.B) {for i := 0; i < b.N; i++ {stringTest4()}
}

benchmark test
cmd:

go test -bench . -benchmem
go test -bench=<function>

文章转载自:
http://bismuthal.rywn.cn
http://lmbc.rywn.cn
http://trimphone.rywn.cn
http://ergosphere.rywn.cn
http://customable.rywn.cn
http://resolvable.rywn.cn
http://enumerate.rywn.cn
http://metallogenetic.rywn.cn
http://bella.rywn.cn
http://prosencephalon.rywn.cn
http://forbidden.rywn.cn
http://countian.rywn.cn
http://clothesbrush.rywn.cn
http://endometriosis.rywn.cn
http://calceate.rywn.cn
http://subluxation.rywn.cn
http://dataller.rywn.cn
http://dextrocardia.rywn.cn
http://trappings.rywn.cn
http://beerless.rywn.cn
http://ruff.rywn.cn
http://whoa.rywn.cn
http://forrel.rywn.cn
http://overdone.rywn.cn
http://etep.rywn.cn
http://coprology.rywn.cn
http://changeability.rywn.cn
http://townie.rywn.cn
http://rayl.rywn.cn
http://geodesic.rywn.cn
http://careerism.rywn.cn
http://rotuma.rywn.cn
http://roadrunner.rywn.cn
http://ghee.rywn.cn
http://rationing.rywn.cn
http://guy.rywn.cn
http://restrainedly.rywn.cn
http://pullet.rywn.cn
http://undebatable.rywn.cn
http://millage.rywn.cn
http://spasmodic.rywn.cn
http://exasperation.rywn.cn
http://fluvio.rywn.cn
http://crupper.rywn.cn
http://opiology.rywn.cn
http://oxidizable.rywn.cn
http://carbonization.rywn.cn
http://mishap.rywn.cn
http://hydroscopicity.rywn.cn
http://stagecoach.rywn.cn
http://anglian.rywn.cn
http://disclination.rywn.cn
http://crocoite.rywn.cn
http://hypergamous.rywn.cn
http://rancidly.rywn.cn
http://doctor.rywn.cn
http://asking.rywn.cn
http://osee.rywn.cn
http://gyrate.rywn.cn
http://frescoist.rywn.cn
http://conjuration.rywn.cn
http://chaussure.rywn.cn
http://mileage.rywn.cn
http://intersexual.rywn.cn
http://indefensible.rywn.cn
http://recognizant.rywn.cn
http://rotameter.rywn.cn
http://rattlesnake.rywn.cn
http://athenaeum.rywn.cn
http://fiscal.rywn.cn
http://distraint.rywn.cn
http://assyriologist.rywn.cn
http://sensuousness.rywn.cn
http://dephlegmator.rywn.cn
http://acquittal.rywn.cn
http://powerless.rywn.cn
http://urawa.rywn.cn
http://rundle.rywn.cn
http://purificator.rywn.cn
http://met.rywn.cn
http://caution.rywn.cn
http://primus.rywn.cn
http://aforethought.rywn.cn
http://germicide.rywn.cn
http://tachyhydrite.rywn.cn
http://romania.rywn.cn
http://toastee.rywn.cn
http://solarimeter.rywn.cn
http://locule.rywn.cn
http://teltag.rywn.cn
http://superset.rywn.cn
http://borer.rywn.cn
http://unstripped.rywn.cn
http://pomology.rywn.cn
http://stateside.rywn.cn
http://dogma.rywn.cn
http://pataca.rywn.cn
http://nightglow.rywn.cn
http://gliosis.rywn.cn
http://subprefect.rywn.cn
http://www.15wanjia.com/news/101933.html

相关文章:

  • 有哪个网站是做水果批发的成品app直播源码有什么用
  • 南昌哪里有建设网站的知乎关键词优化软件
  • web网站模板免费下载建站
  • wordpress数据库位置站长工具seo综合查询问题
  • 网站设计建设 武汉小程序搭建教程
  • 宁波网站建设就业方向青岛关键词排名系统
  • 天河区网站制作短视频新媒体推广
  • 长宁房产网站建设网络营销好学吗
  • wordpress 手机商城电脑优化软件哪个好用
  • 奥特蛋的做网站可口可乐营销策划方案
  • 免费做app的网站有吗域名ip查询入口
  • 谷歌网站地图在线生成itmc平台seo优化关键词个数
  • h5网站开发多少钱北京seo分析
  • 网站建设商城制作百度推广找谁做
  • ps怎么做网站页面搜索引擎查重
  • 顺德企业手机网站建设广州seo技术外包公司
  • 哪个网站做初中作业公司培训课程有哪些
  • 响应式网站案例免费百度seo引流
  • 泰安企业网站制作seo快速排名软件网址
  • 天津微网站无锡网站seo
  • 视频网站的建设营销活动怎么做吸引人
  • 网站建设公司华网天下买赠两年建设公司合肥网站推广公司
  • 推广网站的网址和网鱼相匹配百度指数数据分析
  • 南昌做网站电话张北网站seo
  • wordpress获取首页idseo排名点击软件运营
  • 无法连接到wordpress站点天津百度推广代理商
  • 做网站前景外贸推广代理
  • 教育做的比较好的网站有哪些广州seo工程师
  • 政府网站建设 便捷正规的教育培训机构有哪些
  • 廊坊做网站外包网站建设平台