Pause in a loop

Discussion in 'Python' started by manuel, Jul 22, 2003.

  1. manuel

    manuel Guest

    I've two modules: main.py and eval.py

    Eval.py is imported in main.py, but I've a loop
    in eval.py that must call a function in main.py...

    I can't import main.py in eval.py too, because
    that would create an import loop.

    But I'm thinking a different solution:
    a pause in a loop to return periodically an
    output. It's possible?


    thanks,

    Manuel
     
    manuel, Jul 22, 2003
    #1
    1. Advertising

  2. "manuel" <> schrieb im Newsbeitrag
    news:%W7Ta.10360$...
    >
    > I've two modules: main.py and eval.py
    >
    > Eval.py is imported in main.py, but I've a loop
    > in eval.py that must call a function in main.py...

    ......

    How is that loop activated in the first place? If you activate it by a call
    just provide your main-proc as a callback parameter in that call...

    Kindly
    Michael
     
    Michael Peuser, Jul 22, 2003
    #2
    1. Advertising

  3. manuel

    Jp Calderone Guest

    On Tue, Jul 22, 2003 at 10:00:27AM +0000, manuel wrote:
    >
    > I've two modules: main.py and eval.py
    >
    > Eval.py is imported in main.py, but I've a loop
    > in eval.py that must call a function in main.py...
    >
    > I can't import main.py in eval.py too, because
    > that would create an import loop.
    >
    > But I'm thinking a different solution:
    > a pause in a loop to return periodically an
    > output. It's possible?
    >


    Generators, or "resumable functions" may be one possible solution for
    this:

    from __future__ import generators # Necessary until Python 2.3

    class MoreWorkToDo:
    pass

    class NearlyDone:
    pass

    def f1():
    for i in xrange(100):
    do_something_expensive_and_slow()
    yield MoreWorkToDo
    for i in xrange(10):
    cleanup_a_little()
    yield NearlyDone

    def f2():
    for x in f1():
    print str(x),

    Another possibility is to use a callback:

    class MoreWorkToDo:
    pass

    class NearlyDone:
    pass

    def f1(perLoop):
    for i in xrange(100):
    do_something_expensive_and_slow()
    perLoop(MoreWorkToDo)
    for i in xrange(10):
    cleanup_a_little()
    perLoop(NearlyDone)

    def cbFunc(v):
    print str(v),

    def f2():
    f1(cbFunc)

    Still another approach might create an iterator out of an explicit state
    machine:

    class MoreWorkToDo:
    pass

    class NearlyDone:
    pass

    class Worker:
    def __iter__(self):
    return _WorkerIter()

    class _WorkerIter:
    def __init__(self):
    self.state = 'working'
    self.workCount = 100
    self.cleanCount = 10

    def next(self):
    return getattr(self, 'state_' + self.state)()

    def state_working(self):
    do_something_expensive_and_slow()
    self.workCount -= 1
    if self.workCount <= 0:
    self.state = 'cleanup'
    return MoreWorkToDo

    def state_cleanup(self):
    cleanup_a_little()
    self.cleanCount -= 1
    if self.cleanCount <= 0:
    raise StopIteration()
    return NearlyDone

    def f2():
    for x in Worker():
    print str(x),

    The iterator solution is very similar to the generator solution (in fact,
    the generator also creates an iterator, just a different kind) but as you
    can see is a bit more code. The advantage is the iterator version may be
    able to continue if an unexpected exception is raised in one of the calls to
    `next', whereas the generator form will never be able to do so. The
    iterator version is also pickleable.

    Hope this helps,

    Jp
    >
    > thanks,
    >
    > Manuel
    >
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list


    --
    "If you find a neighbor in need, you're responsible for serving that
    neighbor in need, you're responsible for loving a neighbor just like you'd
    like to love yourself." -- George W. Bush, Sept. 16, 2002
     
    Jp Calderone, Jul 22, 2003
    #3
  4. "manuel" <> a écrit dans le message de news:
    %W7Ta.10360$...
    >
    > I've two modules: main.py and eval.py
    >
    > Eval.py is imported in main.py, but I've a loop
    > in eval.py that must call a function in main.py...
    >
    > I can't import main.py in eval.py too, because
    > that would create an import loop.
    >
    > But I'm thinking a different solution:
    > a pause in a loop to return periodically an
    > output. It's possible?
    >
    >
    > thanks,
    >
    > Manuel
    >
    >


    Send that function to the eval as parameter...

    <main.py>
    ....
    import eval

    def myfunc(...)
    ...
    def main():
    ...
    eval.eval(x, y, z, myfunc)
    ....
    </main.py>

    <eval.py>
    ....
    def eval(x, y, z, afunc):
    ...
    afunc(...)
    ....
    return stuff
    ....
    </eval.py>
     
    Gilles Lenfant, Jul 22, 2003
    #4
  5. manuel

    manuel Guest

    manuel, Jul 22, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Shohet
    Replies:
    10
    Views:
    1,937
    Kevin Spencer
    Jun 18, 2004
  2. GrantMagic

    pause a loop

    GrantMagic, Jul 28, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,991
  3. Leah

    Pause a web page?

    Leah, Mar 15, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    3,193
    Steve C. Orr [MVP, MCSD]
    Mar 15, 2006
  4. Rick
    Replies:
    4
    Views:
    41,677
    Roedy Green
    Oct 8, 2003
  5. Isaac Won
    Replies:
    9
    Views:
    398
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page