in reply to Re: Thread weirdness
in thread Thread weirdness
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Thread weirdness
by zentara (Cardinal) on Sep 25, 2008 at 11:37 UTC | |
In your original node you identified the problem as: One of the functions connects to a web page and parses the content So it dosn't matter if you are an IRC bot, it is still trying to access a web server thru a socket, so you need to deal with the http protocols. This is the most basic ....notice you need to send a Get, or at least a "\n\n", before the web server will respond.
I'm not really a human, but I play one on earth Remember How Lucky You Are | [reply] [d/l] |
by j0nny (Initiate) on Sep 25, 2008 at 12:10 UTC | |
Right now I private message it to login: Now I go back to the channel: I wait about five seconds for it to send the title of google.com back to me, it doesn't send so i just send some random text. Here's the full bot
| [reply] [d/l] [select] |
by Corion (Patriarch) on Sep 25, 2008 at 12:20 UTC | |
I've looked through the code you (finally) posted. I think at least part of the problem is that $Sock is not declared as shared and you will thus have problems with potential race conditions, at least as far as I understand the Perl threading model. I would restrict all communication within your program to Thread::Queues and have one (and only one) thread specialized to talking to the server and listening to replies. That is, I would turn your send_msg function into one that stuffs the message into a queue, and at the other end of that queue have a thread that does nothing but listen to that queue and forward the things to the server. Ideally, no background process would do direct communication with the irc server, they would all deliver their results to the server thread which then handles the communication. You will need send rate limiting anyway so your bot does not get kicked off the server for flooding it. Personally, I would look towards AnyEvent::IRC or any of the other IRC frameworks though. | [reply] [d/l] [select] |
by BrowserUk (Patriarch) on Sep 25, 2008 at 14:41 UTC | |
by BrowserUk (Patriarch) on Sep 25, 2008 at 14:39 UTC | |
Caveat: Most of the contents of this post will only relate to you if you are using Win32. (Although the first bit about not being able to send on a socket from one thread whilst another thread is attempting to read from it may also be true on *nix for blocking sockets?) The problem is that your are trying to both read and write to $Sock concurrently from two threads: In your main thread:
And in your Web thread, several instances of: &send_msg($Channel,"WEB FUNCTION THREAD STARTED"); which becomes:
As your threads are blocking, the writes SendMsg() is blocked until the read in the main thread completes. (Ie. some input arrives). One solution is to set your socket non-blocking. (If your using Win32 Super search for '0x8004667e' for the collective wisdom on Win32 non-blocking sockets). However, this has a fairly profound knock-on for the architecture of your app. A better solution is to only enter a read state when you know that there is something to be read. Unfortunately, the Win32 API that permits this is not directly available from Perl. Even if you access it directly (via: Win32::API), obtaining the appropriate Win32 system handle required to use it, , from the Perl level sockets, is entirely non-trivial. Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
by j0nny (Initiate) on Sep 26, 2008 at 14:59 UTC | |
by BrowserUk (Patriarch) on Sep 26, 2008 at 18:01 UTC | |
| |