Python sleep() function freezes MUSHclient

Posted by Drakonik on Mon 31 Mar 2008 06:05 PM — 5 posts, 24,806 views.

#0
I've written a script which uses the sleep() function from the time module. Some of my functions only sleep for a five seconds, which doesn't seem to cause any problems, but when this particular function sleeps, it does so for 23 seconds, and this causes MUSHclient to freeze entirely (Not Responding). I've only run the script all the way through once; it froze up for the duration of the script (23 seconds times the 22 times the loop was executed), but was back to normal after. However, I experimented with the function a second time, only running one iteration of the loop (23 seconds) and the client froze for several minutes, and when it seemed like it wouldn't return to normal, I terminated its process via the Task Manager.

Below is the function I use.

def study(pages):
	i = 1
	while i < pages:
		world.send("flip book to %s" % i)
		world.send("study book")
		i += 1
		sleep(21)


I've got experience writing code. Just not good code. There may be some kinda of inefficiency or unnecessary steps in the code. My main question is: Is MUSH supposed to freeze for the duration of the sleep() function? If so, how can I prevent it from doing so?
Amended on Mon 31 Mar 2008 06:07 PM by Drakonik
Australia Forum Administrator #1
See http://mushclient.com/faq point 25.
#2
Right. I keep getting pointed to the FAQ. And I keep forgetting. Alright, thanks.
Australia Forum Administrator #3
Your script might look like this:


def study(pages):
  i = 1
  while i <= pages:
    if i == 1:
      world.Send ("flip book to %s" % i)
      world.Send ("study book")
    else:
      world.DoAfter ((i - 1) * 21, "flip book to %s" % i)
      world.DoAfter (((i - 1) * 21) + 1, "study book")
    
    i += 1


The first time through the loop it sends the command to read the first page, afterwards it waits (i - 1) * 21 seconds, so effectively it is queuing up a whole heap of timers (which you will see in the Timers window) set to go off at the correct time.

If you don't want to read all the pages you could go to the Timers configuration window and remove them.

I changed "while i < pages" to "while i <= pages" - I thought your version would stop one page too soon.

[EDIT] - I added another second to the "study book" line, otherwise there was no guarantee that it would flip first and study second.
Amended on Mon 31 Mar 2008 09:57 PM by Nick Gammon
#4
I figured the loop out, thanks to a bit of help from Nick, and for the sake of other people who might read this thread, I wanted to mention something I learned.

When I first created this loop, I had a hell of a time trying to figure out how to make two separate timers that would be close enough to do two separate things, without overlapping. That didn't work so well.

It's possible to use newlines in your scripts with the '\r\n' string. This means that I was able to simple add ONE timer with 'flip book to page\r\nstudy book' instead of having to try to time a bunch of things correctly.

Just something that might come in handy for people in similar situations.