- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我制作了一个Perl脚本,该脚本包装了另一个工具(overlapFeatures),以便可以即时正确地转换文件格式。我正在处理的文件都是制表符分隔的表,通常为200万行左右。就其本身而言,overlapFeatures可以轻松应对这些问题。
但是我认为我正在通过一次管道输送这么多管线来导致管道锁定。我知道我需要以某种方式对此进行线程化,以便可以同时读取和写入子进程。但是我真的不明白如何在perl(或其他任何程序)中正确使用线程。据我了解,我可以使用threads
甚至IPC::run
来解决我的问题。
我最终死锁的原始脚本如下所示:
use strict;
use warnings;
use IPC::Open2;
my $infile = shift;
my $featurefile = shift;
my $command = 'overlapFeatures';
my @args = (qw (-a stdin -b), $featurefile);
my ($input, $output);
my $pid = open2($output, $input, $command, @args)
or die "Failed with error $!\n";
open (my $infh, '<', $infile) or die "Can't open $infile\n";
while (<$infh>){
# Do some format conversion...
chomp
my @cols = split /\t/;
# print a modified line to the tool
print $input join ("\t", @cols[0,2,3,1,5,4]),"\n";
}
close ($input);
while (<$output>){
# format conversion for ouput
chomp;
my @cols = split /\t/;
print join (",",@cols[0,1,2,5,3,8]),"\n";
}
close ($output);
use strict;
use warnings;
use IPC::Open2;
use threads;
my $infile = shift;
my $featurefile = shift;
my $command = 'overlapFeatures';
my @args = (qw (-a stdin -b), $featurefile);
my ($input, $output);
my $pid = open2($output, $input, $command, @args)
or die "Failed with error $!\n";
my $thread = async {
print join(",", qw(seqid start end strand read feature name)),"\n";
for(;;) {
my $line = <$output>; # should block here and wait for output?
last if !defined $line; # end of stream reached?
print STDERR "Got line $line\n";
# Do some format conversion...
chomp $line;
my @cols = split /\t/, $line;
# print a modified line to the tool
print join(",",@cols[0,1,2,5,3,8]),"\n";
}
close($output)
};
{
open (my $infh, '<', $infile) or die "Can't open $infile\n";
while (<$infh>){
# format conversion for ouput
chomp;
my @cols = split /\t/;
print $input join ("\t", @cols[0,2,3,1,5,4]),"\n";
}
close ($input);
}
$thread->join();
waitpid ($pid, 0);
IPC::run
的问题。
$output
检索行。但是,该脚本永远不会结束,并且在收到所有输出后似乎会挂起。我认为这是我现在唯一的问题。
最佳答案
这更像是长篇评论。
我在简化版本中尝试了您的代码。我删除了转换代码,使用Unix yes
命令作为无限数据源,并将输出打印到/dev/null
,因为我们当前对输出不感兴趣,但对程序的工作不感兴趣。作为您的overlapFeatures
的替代品,我使用了cat
将数据原封不动地传递。
use strict; use warnings; use IPC::Open2; use threads;
my $command = "cat";
my @args = ();
my ($input, $output);
my $pid = open2($output, $input, $command, @args)
or die "Failed with error $!\n";
my $thread = async {
print $_ while defined($_ = <$output>);
close($output)
};
{
my $c=0;
open (my $infh, "-|", "yes") or die;
open my $null, ">/dev/null" or die;
while (<$infh>){
$c++;
print $null $_;
if ($c >= 1_000_000) {
print "\n==another million==\n\n";
$c=0
}
}
close ($input);
}
$thread->join();
waitpid ($pid, 0);
一旦达到一百万行(按字面意思),我就会打印一条状态消息以断言IO仍在工作。
yes
输出字符串
"1\n"
,这导致许多行的数据很少) 。(每组约2MB,每行2个字节)
$ convert-to | overlapFeatures | convert-from
这会将所有IPC委派给Shell,并将断言转换正在工作并且该体系结构是可实现的。
close
操作?难道是由于某种奇怪的原因,循环的一端过早退出了吗?在
print STDERR "Closing down xx\n"
之前的
close
可能很有趣。
open2
和
async
是否成功产生了它们的进程/线程并返回了控制流?偏执的我会在他们之后再放一个
print STDERR
...
EOF
d之前,管道不会产生
close
。因此,所有线程都应关闭未使用的所有内容:
my $thread = async {
close $input;
print $_ while defined($_ = <$output>);
close($output)
};
和
{
close $output;
my $c=0;
open (my $infh, "-|", "yes") or die;
open my $null, ">/dev/null" or die;
while (<$infh>){
$c++;
print $null $_;
...
关于multithreading - 使用IPC::open2传输大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12532402/
我有2个功能: function func1() while true do -- listen on connection end end function func2()
我的问题可能看起来很奇怪,但我想我正面临着 volatile 的问题。对象。 我写了一个这样实现的库(只是一个方案,不是真正的内容): (def var1 (volatile! nil)) (def
由于 maven 支持多线程构建,是否可以同时运行 Sonar 多线程? (例如 mvn sonar:sonar -T 4 ) 我运行了它,当模块报告成功时,它报告整个构建失败并返回 java.uti
我们正在启动一个网站,该网站在短时间内的交易量非常大。它基本上是在给票。该代码是用Java,Spring和Hibernate编写的。我想通过产生多个线程并尝试使用JUnit测试用例来获取票证来模仿高容
我正在尝试访问像素数据并将图像从游戏中的相机保存到磁盘。最初,简单的方法是使用渲染目标,然后使用RenderTarget-> ReadPixels(),但是由于ReadPixels()的 native
我们有以下系统: 用户数:〜500k 项目数:〜100k UserSimilarity userSimilarity = new TanimotoCoefficientSimilarity(dataM
也许这是一个经常出现的问题,但我需要根据我的上下文进行一些自定义。 我正在使用 Spring Batch 3.0.1.RELEASE 我有一个简单的工作,有一些步骤。一个步骤是这样的 block :
也许这是一个经常出现的问题,但我需要根据我的上下文进行一些自定义。 我正在使用 Spring Batch 3.0.1.RELEASE 我有一个简单的工作,有一些步骤。一个步骤是这样的 block :
我正在尝试使用PyBrain和Python的multiprocessing软件包在Python中训练神经网络。 这是我的代码(它训练了一个简单的神经网络来学习XOR逻辑)。 import pybrai
我有一个繁重的功能,不适合在主时间轴上执行(因为要花很长时间才能完成并使程序崩溃)。 因此我在air(as3)中搜索多线程,但是我发现的所有示例都说明了如何在worker中运行单独的swf文件。如何在
我想实现线程A 和线程B 并行运行并共享全局变量。 下面是用python编写的代码。我想在中执行相同操作Dart (我不想使用future等待,因为它正在等待其他线程完成或必须等待。) 大小写变量:
我的一个项目只适用于调试 DLL,而不适用于非调试 DLL。 在 Debug DLL 设置下发布项目有哪些注意事项?例如,是否丢失了某些优化? 如何通过将调试版本设置为非调试 DLL 来调试此项目?我
我正在尝试比较 Matlab 和 Julia 之间的速度和性能。我正在查看一个代码,该代码对承受给定负载的连续体结构进行拓扑优化。我正在查看的代码是公共(public)代码topopt88.m:htt
Serving Flask 应用程序“服务器”(延迟加载) 环境:生产警告:这是一个开发服务器。不要在生产部署中使用它。请改用生产 WSGI 服务器。 Debug模式:开启 在 http://0.0.
我对 PyQT 很陌生。我正在学习如何制作 Progressbar 并随着算法的进展对其进行更新。我已经能够制作一个使用此链接进行 self 更新的基本进度条:Python pyqt pulsing
我正在尝试指定在特定线程上运行任务,这样我就可以使用两个专用于“放入” channel 的耗时任务的线程,而其他线程则用于处理该任务。 我对如何将特定任务分配给特定线程感到困惑。我以为我可以使用类似
我正在编写一个软件,它对很多(潜在的大)图像进行大量图像操作/合成。 多线程有助于提高速度,但 QT 不允许同时在同一图像上使用多个 QPainter。 所以我必须在副本的每个线程中进行图像操作/合成
此脚本读取 url 文件以执行多线程 HTTP 请求。 如何使用带有 url 的数组来发出多线程请求? 我的阵列将有类似的东西: @array = ("https://example.com/xsd"
Java 文档声明了以下关于构造函数同步的内容: Note that constructors cannot be synchronized — using the synchronized keyw
我有一个程序,其中主线程创建了很多线程。它崩溃了,我正在调试核心文件。崩溃发生在其中一个子线程中。为了找到原因,我需要知道主线程是否还活着。有什么方法可以找出哪个线程是初始线程? 最佳答案 Is th
我是一名优秀的程序员,十分优秀!