threads not executing @ same time


M

mem

Hi,

Im new to threading with perl. Im writing an irc bot to fetch rss
headers and post new releases from sites like thepiratebay.org

What happens is when some1 types one of the triggers !help,
!list_filters, !info it creates the thread no probs, but it seems to
work just like a sub.
Ie if another person types another trigger they wont see any info until
the 1st thread finishes.

What I would like to happen is for each thread to start when its
called, any information will be greatly appreciated.

Code below;

# create irc connection in thread

my $thread = threads->create('irc_connect');
$thread->join();

sub irc_connect
{
print "\n### Connecting to irc server: $server\n\n";

our $sock = new IO::Socket::INET
(
PeerAddr => $server,
PeerPort => 6667,
Proto => 'tcp'
) or die "!!! Error Couldnt connect to $server\n";

# Send login informatio

print $sock "NICK $nick\r\n";
print $sock "USER $login 8 * :argghhh\r\n";


# wait for login to be confirmed

while ($main::input = <$sock>)
{
if ($main::input =~ /004/)
{
# 004 = login confirmed
last;
}
elsif ($main::input =~ /433/)
{
die "Nickname is already in use.";
}
elsif ($main::input =~ /^PING \:(.*)$/i)
{
# We must respond to PINGs to avoid being disconnected.
print $sock "PONG $1\r\n";
print "*** PONG :$1\r\n";
}
}

print "### Connected to $server\n\n";

# Join the channel.

#print $sock "JOIN #glug\r\n";

$i = 0;
for (@channels)
{
$i++;
print "\n### JOIN $_\r\n";
print $sock "JOIN $_\r\n";
$speak_mode{$_} = 1;
$sum_mode{$_} = 1;
$rel_mode{$_} = 1;
$chan_filter{$_} = "$i.filter.txt";
}


if(-f $settings_file)
{
do $settings_file;
}

while ($main::input = <$sock>)
{ # check irc input and reply as needed
chop $main::input;

&main::check_input($main::input);
}
exit;
}

# checks irc input and preforms replys

sub check_input {
my $input = shift;
chomp $input;

# show info

# :[email protected] PRIVMSG #glug :!info

elsif($input =~ /^\:(.*?)\!.*? PRIVMSG (\#.*?)\s+\:\!info/)
{
@arr = ();
$user = lc $1;

for $chan(@channels)
{
push @arr, "$chan :";
push @arr, "speak mode = ".$speak_mode{$chan};
push @arr, "sum mode = ".$sum_mode{$chan};
push @arr, "rel mode = ".$rel_mode{$chan};
}

&speak_arr_thr($user, @arr);
}
elsif($input =~ /^\:(.*?)\!.*? PRIVMSG (\#.*?)\s+\:\!help/)
{
$user = lc $1;
&speak_arr_thr($user, @main::help);
}

# List filters

# :[email protected] PRIVMSG #glug :!list_filters

elsif($input =~ /^\:(.*?)\!.*? PRIVMSG (\#.*?)\s+\:\!list_filters/)
{
$i = 0;
@arr = ();
$user = lc $1;
$chan = lc $2;
@filters = &readf($main::chan_filter{$chan});
for(@filters)
{
$i++;
push @arr, "$i - $_";
}
&speak_arr_thr($user, @arr);
}
}


# speak arr thr

sub speak_arr_thr
{
my ($u, @arr) = @_;

$thread = threads->create(\&speak_arr, $u, @arr);
$thread->join();
# yield();
return;
}

sub speak_arr
{
my ($chan, @arr) = @_;

for my $line(@arr)
{
print $main::sock "PRIVMSG $chan :$line\r\n";
print ">>> $nick \@ $chan | $line\n";
}
}
 
Ad

Advertisements

R

Ralph Moritz

[snip]
What happens is when some1 types one of the triggers !help,
!list_filters, !info it creates the thread no probs, but it seems to
work just like a sub.
Ie if another person types another trigger they wont see any info until
the 1st thread finishes.

What I would like to happen is for each thread to start when its
called, any information will be greatly appreciated.
[snipped code]

I haven't read your code in great detail or even tried to run it. What
caught my eye immediately was that you're joining your threads. When
you call join() on a thread, it causes the current thread to wait for
that thread to finish executing before continuing. If you want them to
run in "parallel" you'll want to use detatch() instead, as in:

$thread = threads->create(\&speak_arr, $u, @arr);
$thread->detach();

HTH,
Ralph
 
Ad

Advertisements

M

mem

I haven't read your code in great detail or even tried to run it. What
caught my eye immediately was that you're joining your threads. When
you call join() on a thread, it causes the current thread to wait for
that thread to finish executing before continuing. If you want them to
run in "parallel" you'll want to use detatch() instead, as in:

$thread = threads->create(\&speak_arr, $u, @arr);
$thread->detach();

Ralph,

Thanks, I did try detach and they do execute @ the same time but then
this situation occurs:

Each thread prints the 1st part of the array sent, then waits for input
newline input on the socket. So unless some1 is typing the threads halt
on the sleep command.
Without sleep command they flood and exit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top