- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 https://superuser.com/questions/482953/read-non-blocking-from-multiple-fifos-in-parallel?answertab=oldest#tab-top 的原始解决方案在磁盘上保存数据的副本。
我现在制作了第二个版本来缓冲内存中的一行。
它可以工作,但它需要在启动之前连接所有先进先出系统。这有效:
window1$ mkfifo {1..100}
window1$ parcat {1..100} | pv >/dev/null
window2$ parallel -j0 'cat bigfile > ' ::: *
window1$ mkfifo {1..100}
window1$ parcat {1..100} | pv >/dev/null
window2$ parallel -j0 'cat bigfile > ' ::: {1..99}
open '+<'
.那解决了上面的问题,但现在它并没有停在EOF上。
#!/usr/bin/perl
use Symbol qw(gensym);
use IPC::Open3;
use POSIX qw(:errno_h);
use Fcntl qw(:DEFAULT :flock);
for (@ARGV) {
open($fh{$_},"<",$_) || die;
# set fh non blocking($fh{$_});
my $flags;
fcntl($fh{$_}, &F_GETFL, $flags) || die $!; # Get the current flags on the filehandle
$flags |= &O_NONBLOCK; # Add non-blocking to the flags
fcntl($fh{$_}, &F_SETFL, $flags) || die $!; # Set the flags on the filehandle
}
while(keys %fh) {
for(keys %fh) {
my($string,$something_read) = non_blocking_read($_);
print $string;
}
# Sleep 1 ms
select(undef, undef, undef, 1/1000);
}
{
my %buffer;
sub non_blocking_read {
my $file = shift;
my $in = $fh{$file};
my $rv = sysread($in, substr($buffer{$file},length $buffer{$file}), 327680);
if (!$rv) {
if($! == EAGAIN) {
# Would block: Nothing read
return(undef,undef);
} else {
# This file is done
close $in;
delete $fh{$file};
my $buf = $buffer{$file};
delete $buffer{$file};
return ($buf,1);
}
}
# Find \n for full line
my $i = (rindex($buffer{$file},"\n")+1);
if($i) {
# Return full line
# Remove full line from $buffer
return(substr($buffer{$file},0,$i),
1,substr($buffer{$file},0,$i) = "");
} else {
# Something read, but not a full line
return("",1);
}
}
}
#!/usr/bin/perl
use Symbol qw(gensym);
use IPC::Open3;
for (@ARGV) {
open($fh{$_},"<",$_) || die;
set_fh_non_blocking($fh{$_});
}
$ms = 1;
while(keys %fh) {
for(keys %fh) {
my($string,$something_read) = non_blocking_read($_);
if($something_read) {
$ms = 0.1;
print $string;
}
}
$ms = exp_usleep($ms);
}
{
my %buffer;
my $ms;
sub non_blocking_read {
use POSIX qw(:errno_h);
my $file = shift;
my $in = $fh{$file};
my $rv = read($in, substr($buffer{$file},length $buffer{$file}), 327680);
if (!$rv) {
if($! == EAGAIN) {
# Would block: Nothing read
return(undef,undef);
} else {
# This file is done
close $in;
delete $fh{$file};
my $buf = $buffer{$file};
delete $buffer{$file};
return ($buf,1);
}
}
#### Well-tested code below
# Find \n or \r for full line
my $i = (::rindex64(\$buffer{$file},"\n")+1) ||
(::rindex64(\$buffer{$file},"\r")+1);
if($i) {
# Return full line
# Remove full line from $buffer
return(substr($buffer{$file},0,$i),
1,substr($buffer{$file},0,$i) = "");
} else {
# Something read, but not a full line
return("",1);
}
}
}
sub rindex64 {
# Do rindex on strings > 2GB.
# rindex in Perl < v5.22 does not work for > 2GB
# Input:
# as rindex except STR which must be passed as a reference
# Output:
# as rindex
my $ref = shift;
my $match = shift;
my $pos = shift;
my $block_size = 2**31-1;
my $strlen = length($$ref);
# Default: search from end
$pos = defined $pos ? $pos : $strlen;
# No point in doing extra work if we don't need to.
if($strlen < $block_size) {
return rindex($$ref, $match, $pos);
}
my $matchlen = length($match);
my $ret;
my $offset = $pos - $block_size + $matchlen;
if($offset < 0) {
# The offset is less than a $block_size
# Set the $offset to 0 and
# Adjust block_size accordingly
$block_size = $block_size + $offset;
$offset = 0;
}
while($offset >= 0) {
$ret = rindex(
substr($$ref, $offset, $block_size),
$match);
if($ret != -1) {
return $ret + $offset;
}
$offset -= ($block_size - $matchlen - 1);
}
return -1;
}
sub exp_usleep {
# Sleep this many milliseconds.
# Input:
# $ms = milliseconds to sleep
# Returns:
# $ms + 10%
my $ms = shift;
select(undef, undef, undef, $ms/1000);
return (($ms < 1000) ? ($ms * 1.1) : ($ms));
}
sub set_fh_non_blocking {
# Set filehandle as non-blocking
# Inputs:
# $fh = filehandle to be blocking
# Returns:
# N/A
my $fh = shift;
$Global::use{"Fcntl"} ||= eval "use Fcntl qw(:DEFAULT :flock); 1;";
my $flags;
fcntl($fh, &F_GETFL, $flags) || die $!; # Get the current flags on the filehandle
$flags |= &O_NONBLOCK; # Add non-blocking to the flags
fcntl($fh, &F_SETFL, $flags) || die $!; # Set the flags on the filehandle
}
最佳答案
此解决方案会打开一个假写入器,一旦接收到任何数据,该写入器就会关闭。它做正确的事情,除了如果输入为空它不会结束:
mkfifo {1..100}
parcat {1..100} &
parallel -j2 echo works '>' {} ::: {1..100}
parcat {1..100} &
# Fails (parcat does not exit)
parallel -j2 cat /dev/null '>' {} ::: {1..100}
#!/usr/bin/perl
use Symbol qw(gensym);
use IPC::Open3;
use POSIX qw(:errno_h);
use IO::Select;
use strict;
my $s = IO::Select->new();
my %fhr;
my %fhw;
for (@ARGV) {
# Open the file with a fake writer that will never write
open(my $fhw,"+<",$_) || die;
# Open the file for real
open(my $fhr,"<",$_) || die;
set_fh_non_blocking($fhr);
$s->add($fhr);
$fhr{$fhr}++;
$fhw{$fhr}=$fhw;
}
my %buffer;
while(keys %fhr) {
for my $file ($s->can_read(undef)) {
my $rv = sysread($file, substr($buffer{$file},length $buffer{$file}), 327680);
if (!$rv) {
if($! == EAGAIN) {
# Would block: Nothing read
next;
} else {
# This file is done
$s->remove($file);
delete $fhr{$file};
print $buffer{$file};
delete $buffer{$file};
# Closing the $file causes it to block
# close $file;
next;
}
}
if($fhw{$file}) {
# We have received data from $file:
# Close the fake writer
close $fhw{$file};
delete $fhw{$file};
}
# Find \n or \r for full line
my $i = (::rindex64(\$buffer{$file},"\n")+1) ||
(::rindex64(\$buffer{$file},"\r")+1);
if($i) {
# Print full line
# Remove full line from $buffer
print substr($buffer{$file},0,$i);
substr($buffer{$file},0,$i) = "";
next;
} else {
# Something read, but not a full line
next;
}
}
}
sub rindex64 {
# Do rindex on strings > 2GB.
# rindex in Perl < v5.22 does not work for > 2GB
# Input:
# as rindex except STR which must be passed as a reference
# Output:
# as rindex
my $ref = shift;
my $match = shift;
my $pos = shift;
my $block_size = 2**31-1;
my $strlen = length($$ref);
# Default: search from end
$pos = defined $pos ? $pos : $strlen;
# No point in doing extra work if we don't need to.
if($strlen < $block_size) {
return rindex($$ref, $match, $pos);
}
my $matchlen = length($match);
my $ret;
my $offset = $pos - $block_size + $matchlen;
if($offset < 0) {
# The offset is less than a $block_size
# Set the $offset to 0 and
# Adjust block_size accordingly
$block_size = $block_size + $offset;
$offset = 0;
}
while($offset >= 0) {
$ret = rindex(
substr($$ref, $offset, $block_size),
$match);
if($ret != -1) {
return $ret + $offset;
}
$offset -= ($block_size - $matchlen - 1);
}
return -1;
}
sub set_fh_non_blocking {
# Set filehandle as non-blocking
# Inputs:
# $fh = filehandle to be blocking
# Returns:
# N/A
my $fh = shift;
$Global::use{"Fcntl"} ||= eval "use Fcntl qw(:DEFAULT :flock); 1;";
my $flags;
fcntl($fh, &F_GETFL, $flags) || die $!; # Get the current flags on the filehandle
$flags |= &O_NONBLOCK; # Add non-blocking to the flags
fcntl($fh, &F_SETFL, $flags) || die $!; # Set the flags on the filehandle
}
关于Perl:读取 fifos 非阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39195311/
Chisel 是否支持设计中的多个时钟?如果我想实现一个异步 fifo,如何在 Chisel 中实现呢? 最佳答案 是的,Chisel 支持设计中的多个时钟。如果你想使用异步fifo,你可以导入模块C
当在单个进程上使用 FIFO 时,看起来两端都打开然后关闭,不可能重用 FIFO。任何重新打开封闭端的尝试都会失败,或者返回的文件描述符无用。 是否有可能解决此问题,还是我们必须保持 FIFO 的两端
考虑以下场景: 名为 test 的 FIFO被 build 。在一个终端窗口 (A) 中,我运行 cat test 。现在可以在窗口 B 中写入并在窗口 A 中获取输出。也可以终止进程 A 并重新启动
是否有某种方法可用于在真正的 FIFO 中使用 Amazon SQS 消息?我尝试从配置延迟消息传递,但这没有帮助。 最佳答案 亚马逊SQS刚刚获得FIFO Queues with Exactly-O
我有一个非常简单的基本程序,它有两个进程,第一个是parent,第二个是child。 子进程应该向 FIFO 写入一些内容。在所有写作工作完成后(在 child 被终止后)。然后父进程应该读取所有
在 C 中使用 epoll,有什么方法可以让从 FIFO 读取的程序检测到写入同一 FIFO 的程序已将其关闭? 我以为这会生成一个 EPOLLHUP 事件,但显然它不会。 例子:我有一个读者和一个作
在 bash 脚本中,我使用管道将命令的输出传递给另一个命令的输入: $ output_com | input_com output_com 比 input_com 慢。在我的脚本中,在input_c
我试图在 3.12 内核中自己实现 FIFO 调度程序,我无法找到 FIFO 文件的位置...就像 rt.c 位于/kernel/sched/rt.c 最佳答案 您在寻找哪个调度程序? Linux 有
这个问题在这里已经有了答案: Implementing FIFO using LIFO (2 个答案) 关闭 6 年前。 我了解使 Push 操作昂贵或 pop 操作昂贵的传统方法。 如何让push
我正在学习 Linux 中的管道编程,但无法理解管道/FIFO 管理。 我写了一个小程序来打开我创建的 FIFO(在执行程序之前,我在终端中执行了 mkfifo newfifo)。然后我反复读取并转储
我有一个写入标准输出的进程,我希望能够在运行 tail -f 时通过 grepping 查找各种字符串来监视输出。执行此操作的一种方法是写入一个普通文件,然后对文件进行尾部搜索一个字符串,然后对另一个
我想将一些数据输出到管道并让其他进程逐行处理数据。这是一个玩具示例: mkfifo pipe cat pipe& cat >pipe 现在我可以输入我想要的任何内容,按下回车后我立即看到同一行。但是如
我正在尝试开发一个程序来播放和录制一些 rtmp 流。该程序是在Qt中开发的。 我正在使用 rtmpdump 和 mplayer。因为两者都在单独的进程中运行,所以我使用 fifo 将流从 rtmpd
所以我开始构建我的本地 iOS 设备环境。我已经完全设置并运行了我的模拟器环境。我已经通过 iOS 开发人员为应用程序正确设置了我的分发和配置文件。在尝试运行该应用程序时,我遵循以下步骤:使用 USB
S3-FIFO 本文作为下一篇缓存文章的预备知识。 背景 基于LRU和FIFO的驱逐 FIFO和LRU都是经典的缓存驱逐算法,在过去几十年中也出现了很多追求更高效率的驱逐算法,如ARC, 2Q
我可以为单向通信创建一个 FIFO。但我该如何进行双向沟通呢?我可以创建一个可以充当发送方的 C 文件,并打开另一个充当接收方的终端。但如何建立双向通信呢?谢谢! 最佳答案 你不能。尝试一个套接字。考
考虑下表: Id Verb Qty Price `1 Buy 6 10.0 `2 Sell 5 11.0 `3 Buy 4 10.0 `4 Sell 3 11.0 `5 Sell 8 9.0 `6
我有两个进程(“发送者”和“接收者”)需要通过 transient 单向 FIFO 通信管道在单台机器上本地进行通信。这是我想要发生的事情(使用更接近 Unix 域套接字的语言): 发送者在已知地址“
好的,所以我正在使用 FIFO,并且我正在尝试构建一个小型库以在 future 的程序中使用。 它应该能够创建一个命名管道,读取它并在其中写入。 我能够完成所有这些功能,但它没有正确读取。问题如下:
数组有循环缓冲区版本吗?假设一次最大推送元素的数量已知,我是否必须派生自己的 FIFO 队列来提高性能? 这是我尝试过的: 循环实现: function CBuf(n) { var ctrPu
我是一名优秀的程序员,十分优秀!