Or in other words, how to create a time delayed function?
I have a python bot that is supposed to send notifications to user's followers upon the usage of certain commands.
For example , if Tim runs the command ' >follow Tom ', all of Tim's followers will be notified in PM's that he followed Tom , and Tom will be notified that Tim followed him.
I have tested this function with a large amount of followers , and the bot remains stable and avoids being kicked from the server , i'm guessing because the for loop adds a delay to each message sent to each follower.
The problem I have is if two user's were to simultaneously run a command that warrant's a notification. The bot gets kicked offline immediately. So what I need is to add an artificial delay before the notification function is run. Time.sleep() , does not work. all it does it freeze the entire program , and hold every command in a queue. (If two user's ran >follow , it would sleep for 2 seconds , and just run both their commands after the delay)
I'm trying to use the threading module in order to replace time.sleep(). My notification function is the following.
#message is the message to be sent, var is the username to use
def notify(message,var):
#SQL connect
dbconfig = read_db_config()
conn = MySQLConnection(**dbconfig)
cursor = conn.cursor()
#choose all of user's followers
cursor.execute('select username from users where notifications=0 and username IN (select follower from followers where followed like "{}")'.format(var))
results = cursor.fetchall()
#for each , send a PM
for result in results:
self.pm.message(ch.User(str(result[0])), message)
conn.close()
So how would I use threading to do this? I've tried a couple ways , but let's just go with the worst one.
def example(_):
username = 'bob'
# _ is equal to args
a = notify("{} is now following {}.".format(username,_),username)
c =threading.Timer(2,a)
c.start()
This will throw a Nonetype error in response.
Exception in thread Thread-1: Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner self.run() File "/usr/lib/python2.7/threading.py", line 1082, in run self.function(*self.args, **self.kwargs) TypeError: 'NoneType' object is not callable
Note: I think this method will work , there will be a lot of users using the bot at once, so until it breaks this seems like a fix.