Mercurial > xib
comparison bot.py @ 51:2737ed5b9003
Tried to improve the XMPP loop, again.
Signed-off-by: Charly COSTE <changaco@changaco.net>
| author | Charly COSTE <changaco@changaco.net> |
|---|---|
| date | Tue, 25 Aug 2009 14:38:30 +0200 |
| parents | dbb7c29a71e2 |
| children | 5aabf124c78d |
comparison
equal
deleted
inserted
replaced
| 50:dbb7c29a71e2 | 51:2737ed5b9003 |
|---|---|
| 28 from bridge import * | 28 from bridge import * |
| 29 from time import sleep | 29 from time import sleep |
| 30 import re | 30 import re |
| 31 import sys | 31 import sys |
| 32 import xml.parsers.expat | 32 import xml.parsers.expat |
| 33 import traceback | |
| 33 | 34 |
| 34 | 35 |
| 35 class bot(Thread): | 36 class bot(Thread): |
| 36 | 37 |
| 37 def __init__(self, jid, password, nickname, error_fd=sys.stderr, debug=False): | 38 def __init__(self, jid, password, nickname, error_fd=sys.stderr, debug=False): |
| 71 | 72 |
| 72 def _xmpp_loop(self): | 73 def _xmpp_loop(self): |
| 73 """[Internal] XMPP infinite loop.""" | 74 """[Internal] XMPP infinite loop.""" |
| 74 while True: | 75 while True: |
| 75 try: | 76 try: |
| 76 try: | 77 if len(self.xmpp_connections) == 1: |
| 77 if len(self.xmpp_connections) == 1: | 78 sleep(0.5) # avoid bot connection being locked all the time |
| 78 sleep(0.5) # avoid bot connection being locked all the time | 79 for c in self.xmpp_connections.itervalues(): |
| 79 for c in self.xmpp_connections.itervalues(): | 80 if hasattr(c, 'lock'): |
| 80 c.lock.acquire() | 81 c.lock.acquire() |
| 81 if c in self.xmpp_connections.itervalues(): | 82 if c in self.xmpp_connections.itervalues(): |
| 82 if hasattr(c, 'Process'): | 83 if hasattr(c, 'Process'): |
| 83 c.Process(0.01) | 84 c.Process(0.01) |
| 84 c.lock.release() | 85 c.lock.release() |
| 85 except RuntimeError: | 86 except RuntimeError: |
| 86 pass | 87 pass |
| 87 except (xml.parsers.expat.ExpatError, xmpp.protocol.XMLNotWellFormed): | 88 except (xml.parsers.expat.ExpatError, xmpp.protocol.XMLNotWellFormed): |
| 88 self.error('=> Debug: received invalid stanza', debug=True) | 89 self.error('=> Debug: received invalid stanza', debug=True) |
| 89 continue | 90 continue |
| 91 except: | |
| 92 self.error('[Error] Unkonwn exception on XMPP thread:') | |
| 93 traceback.print_exc() | |
| 90 | 94 |
| 91 | 95 |
| 92 def _xmpp_presence_handler(self, dispatcher, presence): | 96 def _xmpp_presence_handler(self, dispatcher, presence): |
| 93 """[Internal] Manage XMPP presence.""" | 97 """[Internal] Manage XMPP presence.""" |
| 94 | 98 |
