Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ MUSHclient
➜ Python
➜ Python sleep() function freezes MUSHclient
|
Python sleep() function freezes MUSHclient
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
| Posted by
| Drakonik
(20 posts) Bio
|
| Date
| Mon 31 Mar 2008 06:05 PM (UTC) Amended on Mon 31 Mar 2008 06:07 PM (UTC) by Drakonik
|
| Message
| 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? | | Top |
|
| Posted by
| Nick Gammon
Australia (23,165 posts) Bio
Forum Administrator |
| Date
| Reply #1 on Mon 31 Mar 2008 08:44 PM (UTC) |
| Message
| |
| Posted by
| Drakonik
(20 posts) Bio
|
| Date
| Reply #2 on Mon 31 Mar 2008 08:48 PM (UTC) |
| Message
| | Right. I keep getting pointed to the FAQ. And I keep forgetting. Alright, thanks. | | Top |
|
| Posted by
| Nick Gammon
Australia (23,165 posts) Bio
Forum Administrator |
| Date
| Reply #3 on Mon 31 Mar 2008 09:23 PM (UTC) Amended on Mon 31 Mar 2008 09:57 PM (UTC) by Nick Gammon
|
| Message
| 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. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | Top |
|
| Posted by
| Drakonik
(20 posts) Bio
|
| Date
| Reply #4 on Sun 20 Jul 2008 03:29 PM (UTC) |
| Message
| 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. | | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
21,940 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top