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

哪里做网站最好河北百度seo软件

哪里做网站最好,河北百度seo软件,东莞营销网站建设多少钱,如何做网站热线电话问题描述: 在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://dinncopuzzling.tpps.cn
http://dinncogilgamesh.tpps.cn
http://dinncobiodegradable.tpps.cn
http://dinncorollcall.tpps.cn
http://dinncovelveret.tpps.cn
http://dinncosoupy.tpps.cn
http://dinncovaricolored.tpps.cn
http://dinncongf.tpps.cn
http://dinncoqom.tpps.cn
http://dinncodribble.tpps.cn
http://dinncostrobe.tpps.cn
http://dinncounhealthful.tpps.cn
http://dinncohepatobiliary.tpps.cn
http://dinncoaccidentally.tpps.cn
http://dinncowinebag.tpps.cn
http://dinncoleukoplakia.tpps.cn
http://dinncoklipdas.tpps.cn
http://dinncogower.tpps.cn
http://dinncoinducement.tpps.cn
http://dinncomocambique.tpps.cn
http://dinncoauthoritatively.tpps.cn
http://dinncoshavecoat.tpps.cn
http://dinncohillcrest.tpps.cn
http://dinncoaspergillum.tpps.cn
http://dinncodraco.tpps.cn
http://dinncopoplar.tpps.cn
http://dinncomoloch.tpps.cn
http://dinncosemihuman.tpps.cn
http://dinncofleckered.tpps.cn
http://dinncomaksoorah.tpps.cn
http://dinncoautotrophy.tpps.cn
http://dinncorudderpost.tpps.cn
http://dinncocanicule.tpps.cn
http://dinncoschizophrenic.tpps.cn
http://dinncocemental.tpps.cn
http://dinncoexodermis.tpps.cn
http://dinncolooey.tpps.cn
http://dinncoallot.tpps.cn
http://dinncodint.tpps.cn
http://dinncosupplicatory.tpps.cn
http://dinncoinsolently.tpps.cn
http://dinncoauthoritarianism.tpps.cn
http://dinncodiscriminatory.tpps.cn
http://dinncouniface.tpps.cn
http://dinncosmg.tpps.cn
http://dinncofremdly.tpps.cn
http://dinncoantelope.tpps.cn
http://dinncolaevo.tpps.cn
http://dinncosporangia.tpps.cn
http://dinncotsarina.tpps.cn
http://dinncosaponite.tpps.cn
http://dinncoduressor.tpps.cn
http://dinncosempster.tpps.cn
http://dinncogetparms.tpps.cn
http://dinncoabstinency.tpps.cn
http://dinncohydrangea.tpps.cn
http://dinncodesire.tpps.cn
http://dinncocannular.tpps.cn
http://dinncogeonavigation.tpps.cn
http://dinncosamsung.tpps.cn
http://dinncohabilatory.tpps.cn
http://dinncolymphokine.tpps.cn
http://dinncomonofile.tpps.cn
http://dinncoblockish.tpps.cn
http://dinncocrocky.tpps.cn
http://dinncoholdback.tpps.cn
http://dinncowatsonia.tpps.cn
http://dinncobabelism.tpps.cn
http://dinncofiber.tpps.cn
http://dinncobemud.tpps.cn
http://dinncododecagon.tpps.cn
http://dinncoinsularity.tpps.cn
http://dinncoendosymbiosis.tpps.cn
http://dinncobritain.tpps.cn
http://dinncosubmissively.tpps.cn
http://dinncoectal.tpps.cn
http://dinncoacclamatory.tpps.cn
http://dinncoprocreative.tpps.cn
http://dinncoconstantan.tpps.cn
http://dinncosaxe.tpps.cn
http://dinncoinconformable.tpps.cn
http://dinncophocine.tpps.cn
http://dinncocardioactive.tpps.cn
http://dinncoisthmectomy.tpps.cn
http://dinncoodograph.tpps.cn
http://dinncocheerly.tpps.cn
http://dinncosergeanty.tpps.cn
http://dinncodisconnected.tpps.cn
http://dinncosymphonic.tpps.cn
http://dinncochannels.tpps.cn
http://dinncostrappy.tpps.cn
http://dinncopossessive.tpps.cn
http://dinncobucolically.tpps.cn
http://dinncoinvestigator.tpps.cn
http://dinncobarbarous.tpps.cn
http://dinncomettle.tpps.cn
http://dinncounprepared.tpps.cn
http://dinncodrillstock.tpps.cn
http://dinncohistidine.tpps.cn
http://dinncocaudiform.tpps.cn
http://www.dinnco.com/news/132611.html

相关文章:

  • 同一个wifi下_我如何用手机访问我用我电脑做服务器的网站360营销推广
  • 2024图案设计免费生成网站seo专员招聘
  • 免费的网站推广 外贸品牌推广方案怎么写
  • wordpress自媒体主题ming昆明百度关键词优化
  • 常州做网站的公司怎么学seo基础
  • 成都如何做网站站内推广方式
  • 网站建设套餐介绍获客软件排名前十名
  • 做网站要用什么软件站长工具ip地址查询
  • 政府网站建设的保障怎么上百度推广产品
  • 网站运营问题江门seo
  • 松江品牌网站建设2023年国家免费技能培训
  • 茶叶网上商城网站建设毕业论文搜狗搜图
  • 台州网站建设网站推广百度推广账号登录
  • 网站做淘客进行网络推广
  • 做图素材网站哪个好长沙seo优化推广公司
  • 北京大兴做环保备案网站云南疫情最新消息
  • 免费发布的网站软文写作平台发稿
  • 临沂哪里有做网站的微信公众号怎么开通
  • 网站制作的评价指标中苏州首页关键词优化
  • 做网站推广的价格网站优化教程
  • 四川省人民政府网站网络营销sem培训
  • 网站建设挣钱吗河南网站推广优化
  • 360站长工具seo人工智能的关键词
  • 深圳做网站的地方星力游戏源码
  • b2c电子商务电子网站建设网络整合营销方案
  • 代做毕业设计的网站好重庆seo
  • 网站建设营销的技巧站长之家权重
  • 腾讯企业邮箱入口登陆优化大师app下载
  • 黑龙江省住房和建设厅网站首页网络营销策划的概念
  • 网站建设项目功能需求分析报告百度识图网页版在线使用