Peter Brooks writes: > I'm not sure if this'll interest anybody, but I expect that I'm > going to get some mutual recursion in my simulation, so I needed to ... > returned, then this solution won't help you. Often, though, you're > not interested in what's returned and would just like the routine to > exit once it's called itself, or another process, recursively. > > If that's the case, this solution, using threads, allows you to have > functions call themselves, or each other, indefinitely. It works OK > on a macbook pro and the thread count varies from 2-3, so it handles
... hard to resist ... but somehow I manage ... whew ... A light-weighter way is to have each task end by assigning the next task and returning, instead of calling the next task directly. When a task returns, a driver loop will call the assigned task, which again does a bounded amount of work, assigns the next task, and returns. Tasks can even pass parameters in the same way. Like so, Dr. Fred keeps adding to a pile as long as there is a pile, and Mr. Jim keeps taking from it as long as it's there to take from: from random import choice def fred(): global fun, arg print('Fred adds 1') fun, arg = choice((fred, jim)), arg + 1 def jim(): global fun, arg print('Jim takes 1') fun, arg = choice((fred, jim)), arg - 1 if __name__ == '__main__': fun, arg = choice((fred, jim)), 3 while arg: print('Pile is', arg, end = '\t') fun() else: print('Pile is gone') -- http://mail.python.org/mailman/listinfo/python-list