StreamWriter未正确关闭?(StreamWriter not closing correctly?)
我正确地使用StreamWriter类时遇到了一些麻烦。 我需要发布大约10个对象,但最多只有一对。 但是,在2个帖子之后,第三个帖子没有通过并且超时。 我意识到这是因为你可以拥有的最大连接数为2.但是,我很困惑为什么我在2之后被停止,因为我(想)我正在关闭我的StreamWriter连接。 这是我的代码:
var httpWebRequest = (HttpWebRequest)WebRequest.Create("my url"); httpWebRequest.ContentType = "text/json"; httpWebRequest.Method = "POST"; //var data = json data using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { streamWriter.Write(data); streamWriter.Close(); }
我认为using语句会自动关闭它,但它没有,所以我添加了Close()行。 但是,这似乎没有做任何事情。 调试器在第三次尝试时卡在使用线上。 如果我将servicePointPoint.DefaultConnectionLimit设置为类似于10的东西,那么一切正常,但我很困惑为什么我写的没有。 任何帮助,将不胜感激!
I'm having some trouble using the StreamWriter class correctly. I have about 10 objects I need to post, but only a couple at a time at most. However, after 2 posts, the third does not go through and times out. I realize this is because the max connections you can have is 2. However, I'm confused as to why I am being stopped after 2 since I (think) I am closing my StreamWriter connection. Here is my code:
var httpWebRequest = (HttpWebRequest)WebRequest.Create("my url"); httpWebRequest.ContentType = "text/json"; httpWebRequest.Method = "POST"; //var data = json data using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { streamWriter.Write(data); streamWriter.Close(); }
I thought the using statement would close it automatically, but it didn't, so I added the Close() line. However, this didn't seem to do anything. The debugger gets stuck on the using line on the third try. If I setServicePointManager.DefaultConnectionLimit to something like 10, everything works, but I'm confused why what I have written doesn't. Any help would be appreciated!
原文:
最满意答案
您可以使用此
for
循环:curr=$(date '+%s') for file in WE*.log; do f="${file%.*}" ts=$(date -d "${f:2}" '+%s') (( (curr - ts) > 30*86400 )) && echo "$file" done | tar cvzf backup.tgz -T-
基本上我们在unix秒中获取当前日期时间,然后从文件中提取日期并使用
date
查找它的EPOCH(unix ts)值。最后,我们将2个时间戳的差异与
30*86400
(30天)进行比较并打印文件名。 整个事情通过管道输出到tar
命令来创建tar压缩文件。You can use this
for
loop:curr=$(date '+%s') for file in WE*.log; do f="${file%.*}" ts=$(date -d "${f:2}" '+%s') (( (curr - ts) > 30*86400 )) && echo "$file" done | tar cvzf backup.tgz -T-
Basically we are getting current date-time in unix seconds and then extracting date from file and finding it's EPOCH (unix ts) value using
date
.Finally we are comparing difference of 2 timestamps with
30*86400
(30 days) and printing the filename. Whole thing is piped to atar
command to create a tar zipped file.
相关问答
更多-
在files变量中嵌入引号并不能达到预期效果(参见BashFAQ#50 )。 您需要使用数组: files=() ... files+=(-C "$path" "$file") ... tar -cf ~/Desktop/archive.tar "${files[@]}" 这将执行如下操作: tar -cf ~/Desktop/archive.tar -C "path1" "file1" -C "path2" "file2" ... Embedding quotes in the files v ...
-
Shell命令到tar目录,不包括某些文件/文件夹(Shell command to tar directory excluding certain files/folders)[2022-08-02]
您可以拥有tar的多个排除选项 $ tar --exclude='./folder' --exclude='./upload/folder2' -zcvf /backup/filename.tgz . 等等会奏效 确保在源和目标项之前放置 - 。 You can have multiple exclude options for tar so $ tar --exclude='./folder' --exclude='./upload/folder2' -zcvf /backup/filename.tgz ... -
Gradle将tar文件添加到tar文件中,但不会产生正确的结果(Gradle add tar file to tar file not producing correct results)[2023-08-10]
尝试这个: 替换这个: from (project.distsDir) { include{'tar1.tar'} } 有: from tar1.outputs 你也可以删除dependsOn:'tar1'作为旁注,tar1仍然存在,除非你以某种方式清理它。 Try this: replace this: from (project.distsDir) { include{'tar1.tar'} } with: from tar1.out ... -
Shell脚本tar错误(Shell script tar error)[2022-06-24]
尝试删除双引号: $TAR -zcvf ${BACKUPDIR}/${BFILE} ${DIRS} Try to remove the double quotes: $TAR -zcvf ${BACKUPDIR}/${BFILE} ${DIRS} -
使用Tar恢复Shell(Shell Restore Using Tar)[2021-12-26]
在还原代码中,您需要删除$DESTIN和$BACKUP之间的空格 代码应该是 tar xvpfz $DESTIN/$BACKUP -C / In your restore code code you need to remove the space between $DESTIN and $BACKUP code should be tar xvpfz $DESTIN/$BACKUP -C / -
以下是更新后问题的第三个解决方案: #!/usr/bin/env bash LOGTYPES=$( ls *log* | sed -rn "s/([0-9]{6})[0-9]{2}.*$/\1/p" | sort -u ) # the sed command, item by item: # # s/ search and replace # ([0-9]{6}) block of 6 digits, and store it # [0-9]{2} followed by 2 more digits # ...
-
看起来原始的tarball没有正确归档,正如l'L'l所指出的那样。 似乎tarball在文件heatmap_gen.py包含了自身的副本。 It looks like the original tarball was not archived correctly as pointed out by l'L'l. It appears that the tarball contained a copy of itself within the file heatmap_gen.py.
-
tar命令的Bash脚本包装器需要很长时间才能执行(Bash script wrapper for tar command takes a very long time to execute)[2023-08-28]
提取时可以为tar提供多个文件名参数,所以: /bin/tar xf $tarfile -C $tmp $contents 您的方法必须在tar文件中搜索每个要提取的文件。 如果提取文件需要1-2秒,并且您想要提取50个文件,则需要50-100秒。 通过一次提供所有文件名,它只扫描tarfile一次,并在遇到时提取每个文件。 You can supply multiple filename arguments to tar when extracting, so do: /bin/tar xf $tarf ... -
使用Shell脚本自动将.tar文件解压缩到当前目录(Extract .tar File to Current Directory Automatically with Shell Script)[2023-04-18]
你可以试试: -C, --directory DIR change to directory DIR 和$(dirname "$x")用于文件目录 for x in in /home/davidwright/attachments/*/*.tar do echo "extracting $x" tar -xvf "$x" -C "$(dirname "$x")" done You can try: -C, --directory DIR c ... -
您可以使用此for循环: curr=$(date '+%s') for file in WE*.log; do f="${file%.*}" ts=$(date -d "${f:2}" '+%s') (( (curr - ts) > 30*86400 )) && echo "$file" done | tar cvzf backup.tgz -T- 基本上我们在unix秒中获取当前日期时间,然后从文件中提取日期并使用date查找它的EPOCH(unix ts)值。 最后,我们将2个时间戳 ...