admin管理员组文章数量:1122846
I have to run a p4 sync
based on whether p4 sync -n
has any output. But I want that as soon as the output of p4 sync -n
has even 1 single line, p4 sync
starts, and not wait for whole output of p4 sync -n.
So, let's say, I have the following lines:
p4 sync -n C:\farm\A...
p4 sync -n C:\farm\B...
p4 sync -n C:\farm\C...
p4 sync -n C:\farm\D...
p4 sync -n C:\farm\E...
p4 sync -n C:\farm\F..
.
and p4 sync -n C:\farm\B...
starts giving output, then p4 sync should start there and then, and not wait for further output of p4 sync -n C:\farm\B...
How can this be done?
I have to run a p4 sync
based on whether p4 sync -n
has any output. But I want that as soon as the output of p4 sync -n
has even 1 single line, p4 sync
starts, and not wait for whole output of p4 sync -n.
So, let's say, I have the following lines:
p4 sync -n C:\farm\A...
p4 sync -n C:\farm\B...
p4 sync -n C:\farm\C...
p4 sync -n C:\farm\D...
p4 sync -n C:\farm\E...
p4 sync -n C:\farm\F..
.
and p4 sync -n C:\farm\B...
starts giving output, then p4 sync should start there and then, and not wait for further output of p4 sync -n C:\farm\B...
How can this be done?
Share Improve this question edited Nov 22, 2024 at 11:02 toolic 61.8k19 gold badges79 silver badges126 bronze badges asked Nov 22, 2024 at 10:40 AskQuestionsPAskQuestionsP 3191 gold badge2 silver badges11 bronze badges 1- execute the process with IPC::Run or similar and read only the first line. See the extensive examples in IPC::Run documentation for details. – Steffen Ullrich Commented Nov 22, 2024 at 10:59
2 Answers
Reset to default 4This is an XY problem. There's no benefit to running p4 sync -n
and then immediately running p4 sync
; presumably you're trying to avoid running an "expensive" sync command when there's nothing to sync, but if there's nothing to sync, then p4 sync -n
and p4 sync
are exactly equivalent in terms of performance (it's easy to profile this with the -Ztrack
global flag if you want to prove it to yourself). Your script should just run p4 sync
if the ultimate intent is to sync the workspace anyway.
That said, answering the general question of how you ask "is there ANYTHING to sync" in the most efficient way possible, you want the -m1
flag, which will tell the server to return a maximum of 1 result and stop immediately. E.g.:
C:\Perforce\test>p4 sync -n ...
//stream/main/p4#1 - added as c:\Perforce\test\p4
//stream/main/undo/foo#7 - added as c:\Perforce\test\undo\foo
//stream/main/undo/itest/A1#5 - added as c:\Perforce\test\undo\itest\A1
//stream/main/undo/p1/A#3 - added as c:\Perforce\test\undo\p1\A
//stream/main/undo/p1/B#1 - added as c:\Perforce\test\undo\p1\B
//stream/main/undo/tempobj#3 - added as c:\Perforce\test\undo\tempobj
C:\Perforce\test>p4 sync -n -m1 ...
//stream/main/p4#1 - added as c:\Perforce\test\p4
Not only does this save you having to read and discard a bunch of output you don't want, it's faster on the back end because the server doesn't need to fetch all those database records only for you to discard them (and in the case of your script, of course, it will need to read them all over again when you run the actual sync, which is why you should just run the sync normally in the first place).
Here's a solution:
my $pid = open( my $pipe, "-|", "p4", "sync", "-n", $qfn )
or die( "..." );
sysread( $pipe, my $buf, 1 ); # Wait for one byte or child to exit.
kill TERM => $pid; # Kill the child if it's still running.
close( $pipe ); # Reap the child.
But it probably doesn't work.
Directing p4
's output to something other than a terminal probably causes it to start buffering. Using a pseudo-tty instead of a pipe would avoid that issue.
IPC::Run, Expect and command-line tool unbuffer
provide ways of creating pseudo-ttys.
本文标签:
版权声明:本文标题:perl - Just check if any output is coming after running "p4 sync -n", and not wait for the whole output - Stac 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736304433a1932233.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论