#wait in MUSHclient by python

Posted by Zero on Wed 29 Jul 2009 11:04 PM — 6 posts, 31,010 views.

#0
I wrote a "#wait" like zmud for MUSH:

def foo():
ExecAfter(2, lambda:world.send('next'))
def localfunc():
world.send('next')
ExecAfter(1, localfunc)


funclist = {}
def ExecAfter(time, func):
global funclist
i = 0
if not funclist.has_key(func):
i = len(funclist)
funclist[func] = i
else:
i = funclist[func]
world.DoAfterSpecial(time, 'CallBack(%d)' %i)

def CallBack(i):
func = None
for item in funclist.iteritems():
if item[1] == i:
print item[0]()
return
#1
Usage: (same as "#wait 2000;next" in zMud)
<trigger ...>
<send> ExecAfter(2, lambda:world.send('next')) </send>
</trigger>

<script>
funclist = {}
def ExecAfter(time, func):
global funclist
i = 0
if not funclist.has_key(func):
i = len(funclist)
funclist[func] = i
else:
i = funclist[func]
world.DoAfterSpecial(time, 'CallBack(%d)' %i)

def CallBack(i):
func = None
for item in funclist.iteritems():
if item[1] == i:
print item[0]()
return
</script>
USA #2
For future reference, you can use the built-in function DoAfter, or DoAfterSpecial for MUSHclient.

http://www.gammon.com.au/scripts/function.php?name=DoAfter
http://www.gammon.com.au/scripts/function.php?name=DoAfterSpecial

If you are wanting to pause the python scripting (I'd advise using the MUSHclient function above instead, as the following will pause the entire script) use :


import time as time

print 'Waiting two seconds...'

time.sleep(2)

print 'Two seconds is up!'


Again, that will pause the entire script, probably not what you want. Use MUSH funtion 'DoAfter' and 'DoAfterSpecial'.
#3
time.sleep(2) will cause Mushclient GUI to not respond.
I guess the GUI and scripts are in the same one thread, is it right?

So i use callback function to implement #wait. It's a wrapping function of DoAfterSpecial.
Amended on Sat 01 Aug 2009 12:32 AM by Zero
Australia Forum Administrator #4
Quote:

I guess the GUI and scripts are in the same one thread, is it right?


MUSHclient is mainly a single thread. The MFC libraries implement dialog boxes in another thread, I think, which is why you see text scrolling by even while you are working on the GUI dialogs. Also there is another thread which monitors the script file changing.

But basically it is a single thread, to all intents and purposes.

Let me quote from the SQLite database engine FAQ page:


Threads are evil. Avoid them.



http://www.sqlite.org/faq.html
Netherlands #5
It's most likely the same thread. The reason other windows still get updated is due to the message loop - it merely dispatches the messages to the right window and through those means they still get processed.

[/random post]