954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Delay Function(s?)

So I'm writing a bit of code for use in an IRC bot. I tried to use scheduler to get around the barbaric-ness of calling time.sleep() every time I want the bot to delay for a few seconds. In scheduler you can choose the delay function "for example time.sleep()"-- but I haven't found any mention of other delay functions anywhere on the net.

Being an IRC bot it's important that it can receive pings and distribute pongs when necessary. time.sleep(), however, pauses the entire .py, rendering the ping/pong lines irrelevant and ultimately leading to a ping timeout.

import time
import sched

snip

c=sched.scheduler(time.time, time.sleep)

snip

x=msg
                        if(int(x)>=30):
                            def xsc(x):
                                swrite("JOIN "+chan)
                                privmsg(chan, "I'm back after a "+x+" second sleave!")
                            swrite("PART "+chan)
                            c.enter(float(x), 1, xsc, (x,))
                            c.run()
                        else:
                            privmsg(chan, "At least a 30 second kick, please ;).")
Synthuir
Newbie Poster
5 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 
time.sleep(), however, pauses the entire .py, rendering the ping/pong lines irrelevant and ultimately leading to a ping timeout.

If I understand the question, you would use threading or multiprocessing to run the code that you don't want to sleep/pause. Since it runs as a separate process, time.sleep() won't affect it.

woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

AutoPython did a great tutorial on threading that has been posted in the tutorials section of this forum:
http://www.daniweb.com/tutorials/tutorial238590.html
Hopefully that clears anything up if you needed a hand :)

Paul Thompson
Veteran Poster
1,119 posts since May 2008
Reputation Points: 264
Solved Threads: 183
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: