Posted: Wed Dec 06, 2006 9:24 pm Post subject: MSN.PM and the threads
Hi,
I'm trying to use MSN.PM ; but I would like to have my bot in a different thread.
In fact, I want the main script will launch the thread containing the bot (with MSN.PM), and this main script will sleep a minute before calling MSN.PM to send a message to some user.
For example :
main.pl :
1. start a thread
2. start the bot in the thread
3. sleep 10 secondes
4. send a message to user1@domain.com
5. sleep 10 secondes
6. send a message to user2@domain.com
Please, howa can I do that ? _________________ Math
You don't need threads to do that. Just set up a timer.
Code:
# setup your $msn object
#...
my $called = {}; # only call users once
my $time = time(); # get current time
while (1) {
$msn->do_one_loop; # loop with MSN
if (time() - $time >= 10 && !exists $called->{user1}) {
$msn->call ($user1,'hello');
$called->{user1} = 'yes';
}
if (time() - $time >= 20 && !exists $called->{user2}) {
$msn->call ($user2,'hello');
$called->{user2} = 'yes';
}
# and so-on, if each user is to be IM'd 10 seconds
# apart, use 10,20,30,40,... as in this example, where
# the number is how many seconds since the bot signed
# on
}
If you want this to continuously IM people with no end, I'm not going to help you with that, because we have a word for it: flooding. And flooding is totally uncool. _________________ Current Site (2008) http://www.cuvou.com/
You don't need threads to do that. Just set up a timer.
Ok, but the timer is for the example an for simplify my post.
In reality, there is no timer, but the script run an calculate something with file on the server (and this take time). Then, after that calcul reach a certain value, it'll call the user by IM
Edit : I already have developped the part witch calculate values.
Cer wrote:
If you want this to continuously IM people with no end, I'm not going to help you with that, because we have a word for it: flooding. And flooding is totally uncool.
I agree, but my bot will only IM user that subscribe to that function, and they can unsubscribe when they want.
Ps : I could implement a function in a bot : when user says "!unsubscribe me", then the bot neither call this user.
Is it more clear now ? And really thanks for helping me. _________________ Math
The first concept to understand is that in Perl, threads are run like separate processes, and they can't communicate with eachother. You can use the module threads::shared which can share specific variables, but for large complex data structures (hashes of hashes, hashes of arrays, arrays of hashes, etc) you have to explicitely share each level of the structure. The $msn object is a complex structure and, unless you want to dissect every corner of the $msn object, you won't be able to share it very easily.
My suggestion is to share an array as a kind of queue of events for the main bot thread to react to.
while (1) {
$msn->do_one_loop;
if (scalar(@QUEUE)) {
my $next = shift(@QUEUE);
my ($for,$msg) = $next =~ /^for (.+?): (.+?)$/i;
$msn->call ($for, $msg);
}
}
Notice that I didn't load MSN until I created the other thread. When a new thread is created, a new Perl process is created with the current state of the script. So if you load modules and set variables before creating a new thread, the new thread will have these modules and variables to begin with (but neither of them are shared by default). Since the thread doesn't need to use the MSN module, I didn't load MSN until after the thread was created.
I generally think it's more efficient to have a new thread spawn to do something small (ie check an RSS feed periodically or something) and have the main thread do all the big stuff (run an MSN bot), otherwise you'd have a thread with a lot of stuff in it and then just a small while(1) loop outside the thread to check RSS feeds. But you can do it any way you want to.
This has been written as the MSN.pm has not been developed for a few years now. In addition most of the earlier releases (such as the ones included in templates) do not work. People also started releasing their own little 'tweaks' which added bits of functionality, but since these people were not alway sure what they were doing many of these modules broke easily. _________________ "Help us, Help you" - BotDepot