Skip to content

Instantly share code, notes, and snippets.

@elprup
Created January 16, 2013 01:57
Show Gist options
  • Save elprup/4543960 to your computer and use it in GitHub Desktop.
Save elprup/4543960 to your computer and use it in GitHub Desktop.
non-blocking gtalk bot using multiple process
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# non-blocking gtalk bot using multiple process
#
import sys
import time
from multiprocessing import Pool
from PyGtalkRobot import GtalkRobot
def command_handler(*args):
# doing some stuff, that can be slow
time.sleep(10)
data ={}
data['user'], data['message'] = args[0], args[1]
return data
class MPBot(GtalkRobot):
def __init__(self, pool):
self.pool = pool
GtalkRobot.__init__(self)
#Regular Expression Pattern Tips:
# I or IGNORECASE <=> (?i) case insensitive matching
# L or LOCALE <=> (?L) make \w, \W, \b, \B dependent on the current locale
# M or MULTILINE <=> (?m) matches every new line and not only start/end of the whole string
# S or DOTALL <=> (?s) '.' matches ALL chars, including newline
# U or UNICODE <=> (?u) Make \w, \W, \b, and \B dependent on the Unicode character properties database.
# X or VERBOSE <=> (?x) Ignores whitespace outside character sets
#This method is used to response users.
def command_100_default(self, user, message, args):
'''.*?(?s)(?m)'''
reply = self.pool.apply_async(command_handler, [user, message, args], callback=self.callback)
def callback(self, result):
self.replyMessage(result['user'], result['message'])
if __name__ == "__main__":
pool = Pool(processes=5) # Start a worker processes
bot = MPBot(pool)
bot.setState('available', "some state info")
bot.start("your_account@gmail.com", "your password")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment