![]() |
|
small problem with my code - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Python (https://sinister.li/Forum-Python) +--- Thread: small problem with my code (/Thread-small-problem-with-my-code) |
small problem with my code - yuyboy - 05-16-2014 Hi all. I have my own jabber bot and today i make new plugin which is to send message for all users. my code was working good but i have small problem thats when i give my bot command to send message my bot get stuck and disconected. I know my bot get stuck and disconected because i have more than 2000 users so my bot can not send MSG in the same time for all users. now i need to ask you if there are any method by python to make my code send the MSG for each user after N second i mean bot send MSG for user1 then wait for N second and send for user2 and like that... Hope my idea is clear and this is my code. Code: def send_msg(type, source, parameters):
ADMINFILE = 'modules/xmpp/users.cfg'
fp = open(ADMINFILE, 'r')
users = eval(fp.read())
if parameters:
for z in users:
msg(z, u"MSG from Admin:\n" +parameters)
reply(type, source, u"MSG has been sent!")
else:
reply(type, source, u"Error! please try again.")
register_command_handler(send_msg, 'msg', ['all','amsg'], 0,'Sends a message to all users')RE: small problem with my code - The Real Slim Shady - 05-16-2014 loop through each user in the list and use something like http://www.tutorialspoint.com/python/time_sleep.htm after each message is sent RE: small problem with my code - yuyboy - 05-17-2014 (05-16-2014, 10:32 PM)Geoff Wrote: loop through each user in the list and use something like http://www.tutorialspoint.com/python/time_sleep.htm after each message is sent Hi, Thank you for your reply.:Grin: hmmm i know about Python time sleep() Method but in my code how i can use it to make my bot send MSG for each user after N second? That's what i need:Smile: Thanks again.:Thumbs-Up: RE: small problem with my code - The Real Slim Shady - 05-17-2014 (05-17-2014, 07:57 AM)yuyboy Wrote:(05-16-2014, 10:32 PM)Geoff Wrote: loop through each user in the list and use something like http://www.tutorialspoint.com/python/time_sleep.htm after each message is sent I am not a python coder but based on general programming knowledge I would probably place it right after this code: Code: msg(z, u"MSG from Admin:\n" +parameters)
reply(type, source, u"MSG has been sent!")RE: small problem with my code - yuyboy - 05-17-2014 No, its wrong. If i put time.sleep(x) in that place thats mean when my bot send MSG then sleep for N time after that stop to do my command. Thank you for your time. Hope anyone help me.. RE: small problem with my code - The Real Slim Shady - 05-17-2014 (05-17-2014, 04:22 PM)yuyboy Wrote: No, its wrong. As far as I can tell, like every other language sleep() only pauses the execution of your program. It will never terminate. If your program terminates after a sleep its probably because you are doing something wrong. RE: small problem with my code - yuyboy - 05-17-2014 I don't say my programme will stope i say my bot will stop to do my command. Any way thanks alot bro.. RE: small problem with my code - yuyboy - 05-21-2014 So, no solution for my problem.? RE: small problem with my code - Ex094 - 05-24-2014 @Geoff has recommend the correct solution, Since you are using it inside a loop that iterates through all the members, the sleep function will simply halt for say 10 sec then after 10 sec it will continue to iterate to the other person, it will not exit or break the loop, it will simply halt if for the given time and then continue. So your code would be like: Code: import time
for users in list:
send message to the user
//Halt for 5 seconds before iterating to the next user
time.sleep(5)
//Rest of your codeRE: small problem with my code - yuyboy - 05-25-2014 (05-24-2014, 06:43 AM)Ex094 Wrote: @Geoff has recommend the correct solution, Since you are using it inside a loop that iterates through all the members, the sleep function will simply halt for say 10 sec then after 10 sec it will continue to iterate to the other person, it will not exit or break the loop, it will simply halt if for the given time and then continue. @Ex094 I try it but i got the same problem. please can you edit my code and do what you mean.. Thanks |