Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #25018 > unrolled thread

Socket code not executing properly in a thread (Windows)

Started by"Andrew D'Angelo" <excel@pharcyde.org>
First post2012-07-08 05:55 -0500
Last post2012-07-08 13:01 +0200
Articles 6 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Socket code not executing properly in a thread (Windows) "Andrew D'Angelo" <excel@pharcyde.org> - 2012-07-08 05:55 -0500
    Re: Socket code not executing properly in a thread (Windows) Thomas Jollans <t@jollybox.de> - 2012-07-07 18:12 +0200
      Re: Socket code not executing properly in a thread (Windows) "Andrew D'Angelo" <excel@pharcyde.org> - 2012-07-07 11:33 -0500
    Re: Socket code not executing properly in a thread (Windows) John Nagle <nagle@animats.com> - 2012-07-07 12:17 -0700
      Re: Socket code not executing properly in a thread (Windows) "Andrew D'Angelo" <excel@pharcyde.org> - 2012-07-08 04:03 -0500
        Re: Socket code not executing properly in a thread (Windows) Thomas Jollans <t@jollybox.de> - 2012-07-08 13:01 +0200

#25018 — Socket code not executing properly in a thread (Windows)

From"Andrew D'Angelo" <excel@pharcyde.org>
Date2012-07-08 05:55 -0500
SubjectSocket code not executing properly in a thread (Windows)
Message-ID<jt9ec1$e5s$1@dont-email.me>
Hi, I've been writing an IRC chatbot that an relay messages it recieves as
an SMS.
As it stands, I can retrieve and parse SMSs from Google Voice perfectly, and
print them to the console. The problem lies in actually posting the message
to the IRC channel. Since the SMS checker runs in a thread apart from the
regular chatbot duties, the socket communication also takes place in the
thread. Something even stranger is that the code works on OS X, but not on
WIndows, where my server runs. Here is a code snippet (If it would help, the
full code can be seen here: http://lickitung.it.cx/exe/bot/bot.py):
def checkVoice():

    global upHz

    global CHANNEL

    global mute

    while 1:

        print "Update voice!"

        voice.sms()

        msgitems = [] #Extract all conversations by searching for a DIV with
an ID at top level.

        tree = BeautifulSoup(voice.sms.html)

        conversations = tree.findAll("div",attrs={"id" :
True},recursive=False)

        #parsing code cut for brevity - I know it works, though

                sendPrivateMessage(CHANNEL,message) #as far as I can tell, 
this is failing to execute

time.sleep(upHz)

def sendPrivateMessage(channel, message):#private message send function

    global mute

    if mute == 0:

        IRC.send("PRIVMSG " + channel + " :" + message + "\r\n") #IRC being 
the socket


[toc] | [next] | [standalone]


#25025

FromThomas Jollans <t@jollybox.de>
Date2012-07-07 18:12 +0200
Message-ID<mailman.1895.1341677582.4697.python-list@python.org>
In reply to#25018
On 07/08/2012 12:55 PM, Andrew D'Angelo wrote:

Please set your clock to the correct date and time.

> (If it would help, the
> full code can be seen here: http://lickitung.it.cx/exe/bot/bot.py):

No, it can't.

> def sendPrivateMessage(channel, message):#private message send function
> 
>     global mute
> 
>     if mute == 0:
> 
>         IRC.send("PRIVMSG " + channel + " :" + message + "\r\n") #IRC being 
> the socket

Do you have an error message or something?

Without knowing more about the IRC library you're using, it's hard to
say what the reason behind the platform-dependent behaviour is. However,
it's clear that you should be calling IRC.send from the main thread.
That shouldn't be too hard to achieve, save the message to some sort of
event or outgoing message queue that the main thread processes in sequence.

(This sounds to me like an application better implemented thread-less,
using select() or some method of asynchronous I/O. Just a shame that
Python makes neither at all pleasant and leaves threads as the nicest
option)

Thomas

[toc] | [prev] | [next] | [standalone]


#25026

From"Andrew D'Angelo" <excel@pharcyde.org>
Date2012-07-07 11:33 -0500
Message-ID<jt9okn$7pi$1@dont-email.me>
In reply to#25025
"Thomas Jollans" <t@jollybox.de> wrote in message 
news:mailman.1895.1341677582.4697.python-list@python.org...
> On 07/08/2012 12:55 PM, Andrew D'Angelo wrote:
>
> Please set your clock to the correct date and time.

My BIOS battery has died and I haven't gotten a chance to replace it. Made a 
mistake when setting the date on boot-up. :-\

> Do you have an error message or something?

No, it simply does not send the message.

> Without knowing more about the IRC library you're using

I am sending plain text to the socket, without a library.

> However,
> it's clear that you should be calling IRC.send from the main thread.
> That shouldn't be too hard to achieve, save the message to some sort of
> event or outgoing message queue that the main thread processes in 
> sequence.

Good idea, thanks. 

[toc] | [prev] | [next] | [standalone]


#25028

FromJohn Nagle <nagle@animats.com>
Date2012-07-07 12:17 -0700
Message-ID<jta1v7$v0b$1@dont-email.me>
In reply to#25018
On 7/8/2012 3:55 AM, Andrew D'Angelo wrote:
> Hi, I've been writing an IRC chatbot that an relay messages it receives as
> an SMS.

    We have no idea what IRC module you're using.

> As it stands, I can retrieve and parse SMSs from Google Voice perfectly

    The Google Voice code you have probably won't work once you have
enough messages stored that Google Voice returns them on multiple
pages.  You have to read all the pages.  If there's any significant
amount of traffic, the completed messages have to be moved or deleted,
or each polling cycle returns more data than the last one.

    Google Voice isn't a very good SMS gateway.  I used to use it,
but switched to Twilio (which costs, but works) two years ago.

				John Nagle

[toc] | [prev] | [next] | [standalone]


#25040

From"Andrew D'Angelo" <excel@pharcyde.org>
Date2012-07-08 04:03 -0500
Message-ID<jtbncn$c0c$1@dont-email.me>
In reply to#25028
"John Nagle" <nagle@animats.com> wrote in message 
news:jta1v7$v0b$1@dont-email.me...

>    We have no idea what IRC module you're using.

I am sending plain text to the socket, sorry for not mentioning it before.
At the simplest level, it appears that when the code runs in Windows, data 
is not sent to a socket when inside a thread, while on OS X it is.
Also, running the sned to socket code inside the main loop would not work, 
as the main loop pauses until it recieves data from the socket (an IRC 
message), which is why I didn't put the SMS code in the main loop in the 
first place.

> The Google Voice code you have probably won't work once you have
> enough messages stored that Google Voice returns them on multiple
> pages.

I know that this is not the problem because the code works fine on OS X, but 
not Windows, where I actually need it to run. In addition, each message is 
deleted as it is parsed and no other SMSs are sent to the number, so fill-up 
should not be a problem. 

[toc] | [prev] | [next] | [standalone]


#25041

FromThomas Jollans <t@jollybox.de>
Date2012-07-08 13:01 +0200
Message-ID<mailman.1915.1341745281.4697.python-list@python.org>
In reply to#25040
On 07/08/2012 11:03 AM, Andrew D'Angelo wrote:
> Also, running the sned to socket code inside the main loop would not work, 
> as the main loop pauses until it recieves data from the socket (an IRC 
> message), which is why I didn't put the SMS code in the main loop in the 
> first place.


http://docs.python.org/library/select.html

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web