Perl-创建一个新数组并将其传递给子例程线程 [英] Perl - Create a new array and pass it to a subroutine thread
问题描述
我试图创建一个新的一定长度的多维数组(5,用于测试),用值填充它,然后将该数组传递给在与主代码不同的线程中运行的子例程.主代码可以继续创建新数组,以填充下一个值.这个循环必须不断地继续下去.
I am trying to create a new multi-dimensional array of a certain length (5, for testing), fill it with values and then pass that array to a subroutine that runs in a thread separate from the main code so that the main code can continue to create a new array for next values to be filled. This cycle has to continue endlessly.
我看到数组已传递,并且可以看到$set[0]
的值,但似乎数组被覆盖或类似的东西.我不确定这是怎么回事.而且超表连接对象未正确传递:我必须在每个线程中创建一个新的连接对象.我在这里想念什么?
I see the array is passed and I can see values for $set[0]
, but it seems like the array gets overwritten or something. I'm not sure of what is going on here. And the hypertable connection object is not passed properly: I had to create a new connection object in each thread. What am I missing here?
#!/usr/bin/perl -w -I /opt/hypertable/0.9.7.3/lib/perl -I /opt/hypertable/0.9.7.3/lib/perl/gen-perl
use strict;
use IO::Socket;
use Geo::IP;
use threads qw(stringify);
use Net::NBName;
use Data::Dumper;
use Hypertable::ThriftClient;
my $hypertable = new Hypertable::ThriftClient("Server", 38080);
my $namespace = $hypertable->namespace_open("TEST");
my $MAXLEN = 1524;
my $buf = '';
my $limit = 5; #length of array
my $sock = IO::Socket::INET->new(LocalPort => '514', Proto => 'udp') || die("Socket: $@");
do {
my $count = 0;
my @set;
for ($count = 0; $count <= $limit; $count++) {
$sock->recv($buf, $MAXLEN);
my ($port, $ipaddr) = sockaddr_in($sock->peername);
my $hn = gethostbyaddr($ipaddr, AF_INET);
$buf =~ /<(\d+)>(.*?):(.*)/;
my $msg = $3;
$set[$count][0] = $hn;
$set[$count][1] = $msg;
print $count. " --> "
. $set[$count][0] . " --> "
. $set[$count][1]
. "\n"; #Multi dimensional array
}
my $thr = threads->create('logsys', @set, $hypertable);
} while (1);
sub logsys {
my $count = 0;
for ($count = 0; $count <= $limit; $count++) {
my $hypertable = shift; # Here I want to use the single NoSQL db connector object for all threads
my @set = shift;
print $count. " --> "
. @set->[$count][0] . " --> "
. @set->[$count][1]
. "\n"; # Here I expect the same exact array elements
#DO SOME MORE STUFF here
}
}
编辑:一个简单的代码,可以在线程中运行,也可以在没有线程的情况下运行.在线程中运行时,处理过程不会处理数组中的所有元素.但是,当在没有线程的情况下运行时,它将处理所有元素.
A simple code either to be run in a thread or without thread. When run in a thread the processing doesn't process all the elements in the array. But when run without threading then it processes all the elements.
#!/usr/bin/perl -w -I /opt/hypertable/0.9.7.3/lib/perl -I /opt/hypertable/0.9.7.3/lib/perl/gen-perl
use strict;
use IO::Socket;
use Geo::IP;
use threads qw(stringify);
use Net::NBName;
use Data::Dumper;
use Hypertable::ThriftClient;
# Syslog Variables and Constants
my $MAXLEN = 1524;
my $limit = 5; #for testing
my $sock;
# Start Listening on UDP port 514
$sock = IO::Socket::INET->new(LocalPort => '514', Proto => 'udp') || die("Socket: $@");
my $buf = '';
my $count = 0;
my @set;
for ($count = 0; $count <= $limit; $count++) {
$sock->recv($buf, $MAXLEN);
my ($port, $ipaddr) = sockaddr_in($sock->peername);
my $hn = gethostbyaddr($ipaddr, AF_INET);
$buf=~/<(\d+)>(.*?):(.*)/;
my $msg=$3;
$set[$count][0] = $hn;
$set[$count][1] = $msg;
print $count." --> ".$set[$count][0]." --> ".$set[$count][1]."\n";#Print original array, should print 5 elements
my $thr = threads->create('logsys',@set);
#&logsys(@set);
sub logsys {
my $count = 0;
my @set= @_;
print "--------------------- ".scalar (@set)." -------------------\n";
for ($count=0; $count <= $limit; $count++) {
print $count." --> ".$set[$count][0]." --> ".$set[$count][1]."\n";#print passed array, should same exact 5 elements
if (open(WW,">syslog")){print WW $count." --> ".$set[$count][0]." --> ".$set[$count][1]."\n"; close(WW);}
}
}
作为线程运行时的O/P:
0 --> ids-01p --> 23:48 IDS01 SFIMS: [FLIDS][Enterprise][138:4:1] sensitive_data: sensitive data - U.S. social security numbers without dashes [Classification: Sensitive Data] [Priority: 2] {TCP} 10.10.97.42:3065 -> 33.87.66.38:80
1 --> ids-01p --> 23:50 IDS01 SFIMS: [FLIDS][Enterprise][138:4:1] sensitive_data: sensitive data - U.S. social security numbers without dashes [Classification: Sensitive Data] [Priority: 2] {TCP} 10.10.1.254:26616 -> 78.67.61.202:80
2 --> ids-01p --> 23:50 IDS01 SFIMS: [FLIDS][Enterprise][138:4:1] sensitive_data: sensitive data - U.S. social security numbers without dashes [Classification: Sensitive Data] [Priority: 2] {TCP} 10.10.1.254:39180 -> 56.164.27.51:80
3 --> ids-01p --> 23:51 IDS01 SFIMS: [FLIDS][Enterprise][138:4:1] sensitive_data: sensitive data - U.S. social security numbers without dashes [Classification: Sensitive Data] [Priority: 2] {TCP} 10.10.52.97:53967 -> 173.194.37.97:80
4 --> ids-01p --> 23:51 IDS01 SFIMS: [FLIDS][Enterprise][119:15:1] http_inspect: OVERSIZE REQUEST-URI DIRECTORY [Classification: Potentially Bad Traffic] [Priority: 2] {TCP} 10.190.1.254:57265 -> 34.44.17.21:80
5 --> ids-01p --> 23:51 IDS01 SFIMS: [FLIDS][Enterprise][119:15:1] http_inspect: OVERSIZE REQUEST-URI DIRECTORY [Classification: Potentially Bad Traffic] [Priority: 2] {TCP} 10.190.1.254:41960 -> 34.44.17.29:80
--------------------- 6 -------------------
0 --> ids-01p --> 23:48 IDS01 SFIMS: [FLIDS][Enterprise][138:4:1] sensitive_data: sensitive data - U.S. social security numbers without dashes [Classification: Sensitive Data] [Priority: 2] {TCP} 10.190.97.42:3065 -> 43.87.66.38:80
Perl exited with active threads:
1 running and unjoined
0 finished and unjoined
0 running and detached
1 --> ids-01p --> 23:50 IDS01 SFIMS: [FLIDS][Enterprise][138:4:1] sensitive_data: sensitive data - U.S. social security numbers without dashes [Classification: Sensitive Data] [Priority: 2] {TCP} 10.190.1.254:26616 -> 43.67.61.202:80
在没有线程的情况下运行时的O/P:
0 --> ids-01p --> 36:48 IDS01 SFIMS: [FLIDS][Enterprise][138:4:1] sensitive_data: sensitive data - U.S. social security numbers without dashes [Classification: Sensitive Data] [Priority: 2] {TCP} 10.10.1.254:34053 -> 69.164.26.77:80
1 --> ids-01p --> 36:50 IDS01 SFIMS: [FLIDS][Enterprise][138:4:1] sensitive_data: sensitive data - U.S. social security numbers without dashes [Classification: Sensitive Data] [Priority: 2] {TCP} 10.1.65.51:57977 -> 216.137.41.5:80
2 --> ids-01p --> 36:53 IDS01 SFIMS: [FLIDS][Enterprise][128:4:1] ssh: Protocol mismatch [Classification: Detection of a Non-Standard Protocol or Event] [Priority: 2] {TCP} 10.10.241.46:11120 -> 10.10.125.227:22
3 --> ids-01p --> 36:54 IDS01 SFIMS: [FLIDS][Enterprise][128:4:1] ssh: Protocol mismatch [Classification: Detection of a Non-Standard Protocol or Event] [Priority: 2] {TCP} 10.10.241.46:11122 -> 10.1.125.225:22
4 --> ids-01p --> 36:54 IDS01 SFIMS: [FLIDS][Enterprise][138:4:1] sensitive_data: sensitive data - U.S. social security numbers without dashes [Classification: Sensitive Data] [Priority: 2] {TCP} 10.1.118.96:61686 -> 50.19.254.195:80
5 --> ids-01p --> 36:54 IDS01 SFIMS: [FLIDS][Enterprise][138:4:1] sensitive_data: sensitive data - U.S. social security numbers without dashes [Classification: Sensitive Data] [Priority: 2] {TCP} 10.1.1.254:29437 -> 184.73.178.248:80
--------------------- 7 -------------------
0 --> ids-01p --> 36:48 IDS01 SFIMS: [FLIDS][Enterprise][138:4:1] sensitive_data: sensitive data - U.S. social security numbers without dashes [Classification: Sensitive Data] [Priority: 2] {TCP} 10.10.1.254:34053 -> 69.164.26.77:80
1 --> ids-01p --> 36:50 IDS01 SFIMS: [FLIDS][Enterprise][138:4:1] sensitive_data: sensitive data - U.S. social security numbers without dashes [Classification: Sensitive Data] [Priority: 2] {TCP} 10.1.65.51:57977 -> 216.137.41.5:80
2 --> ids-01p --> 36:53 IDS01 SFIMS: [FLIDS][Enterprise][128:4:1] ssh: Protocol mismatch [Classification: Detection of a Non-Standard Protocol or Event] [Priority: 2] {TCP} 10.10.241.46:11120 -> 10.10.125.227:22
3 --> ids-01p --> 36:54 IDS01 SFIMS: [FLIDS][Enterprise][128:4:1] ssh: Protocol mismatch [Classification: Detection of a Non-Standard Protocol or Event] [Priority: 2] {TCP} 10.10.241.46:11122 -> 10.1.125.225:22
4 --> ids-01p --> 36:54 IDS01 SFIMS: [FLIDS][Enterprise][138:4:1] sensitive_data: sensitive data - U.S. social security numbers without dashes [Classification: Sensitive Data] [Priority: 2] {TCP} 10.1.118.96:61686 -> 50.19.254.195:80
5 --> ids-01p --> 36:54 IDS01 SFIMS: [FLIDS][Enterprise][138:4:1] sensitive_data: sensitive data - U.S. social security numbers without dashes [Classification: Sensitive Data] [Priority: 2] {TCP} 10.1.1.254:29437 -> 184.73.178.248:80
推荐答案
一方面,您正在调用带有参数(..., @set, $hypertable)
的线程构造函数,但是在线程中,您正在使用shift
为$hypertable
赋值在分配@set
之前.您可能想要更改参数的顺序,如
For one thing, you are calling the thread constructor with arguments (..., @set, $hypertable)
but in the thread you are using shift
to assign a value to $hypertable
before you assign @set
. You will either want to change the order of the arguments, as in
threads->create('logsys',$hypertable,@set);
或使用pop
从@_
的末尾删除参数,而不是shift
(将其从开头删除):
or use pop
to remove the argument from the end of @_
instead of shift
(which removes it from the beginning):
my $hypertable = pop; # same as pop(@_)
第二,像@set = shift
这样的赋值几乎总是错误的.将返回值shift
(它是单个标量值)分配给列表是不寻常的.一旦在线程中分配了$hypertable
并从@_
中删除了它的值,@_
中剩下的就是您提供的@set
,所以您可以说
Second, an assignment like @set = shift
is almost always wrong. It is unusual to assign the return value of shift
, which is a single scalar value, to a list. Once you have assigned $hypertable
in the thread and removed its value from @_
, all that remains in @_
is the @set
you provided, so you can just say
my @set = @_;
@set->[...]
在Perl中也没有意义.要访问线程中2d数组@set
的元素,可以使用与创建数组相同的符号:
@set->[...]
is also not meaningful in Perl. To access an element of the 2d-array @set
in the thread, you can use the same notation you used to create the array:
print $count." --> ".$set[$count][0]." --> ".$set[$count][1]."\n";
这篇关于Perl-创建一个新数组并将其传递给子例程线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!