Register forum user name Search FAQ

Gammon Forum

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.
 Entire forum ➜ MUSHclient ➜ Python ➜ Threads causing crash?

Threads causing crash?

It is now over 60 days since the last post. This thread is closed.     Refresh page


Pages: 1  2 

Posted by Nick Gammon   Australia  (23,102 posts)  Bio   Forum Administrator
Date Reply #15 on Wed 13 Jun 2007 10:06 PM (UTC)
Message
See: http://mushclient.com/forum/?id=4956

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Isthiriel   (113 posts)  Bio
Date Reply #16 on Sat 16 Jun 2007 08:40 AM (UTC)

Amended on Sat 16 Jun 2007 01:36 PM (UTC) by Isthiriel

Message
Martijn, the reference implementation of Python (as of 2.5) has coroutines (as an extension of generators). However the nature of generators in Python means that you can't use the exact structure Nick has for lua.

Lua's yield apparently yields all the way out to the encompassing coroutine.resume() call, whereas Python's yield only yields to the containing block, while the function call containing the yield "returns" a generator object encapsulating the function's current execution state.

So the support code looks like:
coroutine_l = {}
coroutine_i = 0

# wraps is a helper-decorator for writing decorators
# it updates __name__, __dict__ and __doc__ as appropriate
from functools import wraps

# decorator for coroutine magic, taken from PEP 342
def wait_coroutine(f):
    @wraps(f)
    def wrapper(*args, **kw):
        global coroutine_i, coroutine_l
        id = "coroutine_%s_%08x" % (f.__name__, coroutine_i)
        coroutine_i += 1
        g = f(*args, **kw)
        coroutine_l[id] = g
        coroutine_resume(id)
        return g
    return wrapper


def coroutine_resume(id):
    global coroutine_i, coroutine_l
    assert coroutine_l.has_key(id), "Could not find '%s' in list of coroutines." % id
    g = coroutine_l[id]
    del coroutine_l[id] # remove the old id from the list
    # the generator will get a new id if it hasn't stopped
    try:
        s = g.next()
    except StopIteration: # generator has stopped execution
        pass
    else: # lodge new oneshot timer
        id = "%s%08x" % (id[:-8], coroutine_i)
        coroutine_i += 1
        coroutine_l[id] = g # give the generator a new id
        # this is necessary as we are currently in the
        # execution of the old timer still so we can't
        # reuse the timer name :)
        s = int(s)
        ss = s % 60
        mm = (s / 60) % 60
        hh = s / 3600
        f = world.AddTimer(id, hh, mm, ss, '',
            eEnabled | eOneShot | eReplace | eTemporary,
            'coroutine_resume')
        assert f == eOK, "Could not AddTimer in coroutine_resume(): %s, %d" % (id, f)


And our actual "coroutine":
@wait_coroutine
def my_alias(name, line, wildcards):
    world.Note("prepare heal")
    yield 1 # wait for 1 sec
    world.Note("cast heal")
    yield 2
    world.Note("eat bread")
    yield 3
    world.Note("Done")


Stackless Python ([url]http://www.stackless.com/[/url]) has even more coroutine-y goodness :)
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.


51,700 views.

This is page 2, subject is 2 pages long:  [Previous page]  1  2 

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.