[Home] [Downloads] [Search] [Help/forum]

Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Python
. . -> [Subject]  Threads causing crash?

Threads causing crash?

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

Pages: 1  2 

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

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Isthiriel   (113 posts)  [Biography] 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

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):
    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
        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
        s = g.next()
    except StopIteration: # generator has stopped execution
    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,
        assert f == eOK, "Could not AddTimer in coroutine_resume(): %s, %d" % (id, f)

And our actual "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

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


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] Refresh page

Go to topic:           Search the forum

[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]